@content-island/api-client 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,9 +1,14 @@
1
+ declare type AllowedModelFields<M extends Model, ValueType> = Partial<Omit<{
2
+ [K in keyof M as M[K] extends string | number | boolean ? `fields.${string & K}` : never]?: ValueType;
3
+ }, 'fields.id' | 'fields.language'>>;
4
+
1
5
  export declare interface ApiClient {
2
6
  getProject: () => Promise<Project>;
3
- getContentList: <M extends Model = Model & Record<string, any>>(queryParam?: QueryParams<M>) => Promise<M[]>;
4
- getContent: <M extends Model = Model & Record<string, any>>(queryParam: QueryParams<M>) => Promise<M>;
5
- getRawContentList: <M extends Model = Model & Record<string, any>>(queryParam?: QueryParams<M>) => Promise<Content[]>;
6
- getRawContent: <M extends Model = Model & Record<string, any>>(queryParam: QueryParams<M>) => Promise<Content>;
7
+ getContentList: <M extends Model = Model & Record<string, any>>(queryParam?: ContentListQueryParams<M>) => Promise<M[]>;
8
+ getContent: <M extends Model = Model & Record<string, any>>(queryParam: ContentQueryParams<M>) => Promise<M>;
9
+ getRawContentList: <M extends Model = Model & Record<string, any>>(queryParam?: ContentListQueryParams<M>) => Promise<Content[]>;
10
+ getRawContent: <M extends Model = Model & Record<string, any>>(queryParam: ContentQueryParams<M>) => Promise<Content>;
11
+ getContentListSize: <M extends Model = Model & Record<string, any>>(queryParam?: ContentListSizeQueryParams<M>) => Promise<number>;
7
12
  }
8
13
 
9
14
  export declare type ClientFilter<Type = string | boolean> = Type | {
@@ -17,6 +22,12 @@ export declare interface Content {
17
22
  fields: Field[];
18
23
  }
19
24
 
25
+ export declare type ContentListQueryParams<M extends Model = Model & Record<string, any>> = QueryParams<M>;
26
+
27
+ export declare type ContentListSizeQueryParams<M extends Model = Model & Record<string, any>> = Omit<QueryParams<M>, 'sort' | 'pagination' | 'includeRelatedContent'>;
28
+
29
+ export declare type ContentQueryParams<M extends Model = Model & Record<string, any>> = Omit<QueryParams<M>, 'sort' | 'pagination'>;
30
+
20
31
  declare interface ContentType extends Lookup {
21
32
  fields: ContentTypeField[];
22
33
  }
@@ -52,6 +63,200 @@ export declare type FieldType =
52
63
  | 'color'
53
64
  | FieldEntityType;
54
65
 
66
+ declare type FilterableFields<M extends Model = Model> = {
67
+ id?: ClientFilter;
68
+ lastUpdate?: ClientFilter;
69
+ language?: ClientFilter<M['language'] extends undefined ? string : M['language']>;
70
+ contentType?: ClientFilter;
71
+ includeRelatedContent?: boolean;
72
+ } & AllowedModelFields<M, ClientFilter>;
73
+
74
+ declare type LanguageCode =
75
+ | 'aa'
76
+ | 'ab'
77
+ | 'ae'
78
+ | 'af'
79
+ | 'ak'
80
+ | 'am'
81
+ | 'an'
82
+ | 'ar'
83
+ | 'as'
84
+ | 'av'
85
+ | 'ay'
86
+ | 'az'
87
+ | 'ba'
88
+ | 'be'
89
+ | 'bg'
90
+ | 'bh'
91
+ | 'bi'
92
+ | 'bm'
93
+ | 'bn'
94
+ | 'bo'
95
+ | 'br'
96
+ | 'bs'
97
+ | 'ca'
98
+ | 'ce'
99
+ | 'ch'
100
+ | 'co'
101
+ | 'cr'
102
+ | 'cs'
103
+ | 'cu'
104
+ | 'cv'
105
+ | 'cy'
106
+ | 'da'
107
+ | 'de'
108
+ | 'dv'
109
+ | 'dz'
110
+ | 'ee'
111
+ | 'el'
112
+ | 'en'
113
+ | 'eo'
114
+ | 'es'
115
+ | 'et'
116
+ | 'eu'
117
+ | 'fa'
118
+ | 'ff'
119
+ | 'fi'
120
+ | 'fj'
121
+ | 'fo'
122
+ | 'fr'
123
+ | 'fy'
124
+ | 'ga'
125
+ | 'gd'
126
+ | 'gl'
127
+ | 'gn'
128
+ | 'gu'
129
+ | 'gv'
130
+ | 'ha'
131
+ | 'he'
132
+ | 'hi'
133
+ | 'ho'
134
+ | 'hr'
135
+ | 'ht'
136
+ | 'hu'
137
+ | 'hy'
138
+ | 'hz'
139
+ | 'ia'
140
+ | 'id'
141
+ | 'ie'
142
+ | 'ig'
143
+ | 'ii'
144
+ | 'ik'
145
+ | 'io'
146
+ | 'is'
147
+ | 'it'
148
+ | 'iu'
149
+ | 'ja'
150
+ | 'jv'
151
+ | 'ka'
152
+ | 'kg'
153
+ | 'ki'
154
+ | 'kj'
155
+ | 'kk'
156
+ | 'kl'
157
+ | 'km'
158
+ | 'kn'
159
+ | 'ko'
160
+ | 'kr'
161
+ | 'ks'
162
+ | 'ku'
163
+ | 'kv'
164
+ | 'kw'
165
+ | 'ky'
166
+ | 'la'
167
+ | 'lb'
168
+ | 'lg'
169
+ | 'li'
170
+ | 'ln'
171
+ | 'lo'
172
+ | 'lt'
173
+ | 'lu'
174
+ | 'lv'
175
+ | 'mg'
176
+ | 'mh'
177
+ | 'mi'
178
+ | 'mk'
179
+ | 'ml'
180
+ | 'mn'
181
+ | 'mr'
182
+ | 'ms'
183
+ | 'mt'
184
+ | 'my'
185
+ | 'na'
186
+ | 'nb'
187
+ | 'nd'
188
+ | 'ne'
189
+ | 'ng'
190
+ | 'nl'
191
+ | 'nn'
192
+ | 'no'
193
+ | 'nr'
194
+ | 'nv'
195
+ | 'ny'
196
+ | 'oc'
197
+ | 'oj'
198
+ | 'om'
199
+ | 'or'
200
+ | 'os'
201
+ | 'pa'
202
+ | 'pi'
203
+ | 'pl'
204
+ | 'ps'
205
+ | 'pt'
206
+ | 'qu'
207
+ | 'rm'
208
+ | 'rn'
209
+ | 'ro'
210
+ | 'ru'
211
+ | 'rw'
212
+ | 'sa'
213
+ | 'sc'
214
+ | 'sd'
215
+ | 'se'
216
+ | 'sg'
217
+ | 'si'
218
+ | 'sk'
219
+ | 'sl'
220
+ | 'sm'
221
+ | 'sn'
222
+ | 'so'
223
+ | 'sq'
224
+ | 'sr'
225
+ | 'ss'
226
+ | 'st'
227
+ | 'su'
228
+ | 'sv'
229
+ | 'sw'
230
+ | 'ta'
231
+ | 'te'
232
+ | 'tg'
233
+ | 'th'
234
+ | 'ti'
235
+ | 'tk'
236
+ | 'tl'
237
+ | 'tn'
238
+ | 'to'
239
+ | 'tr'
240
+ | 'ts'
241
+ | 'tt'
242
+ | 'tw'
243
+ | 'ty'
244
+ | 'ug'
245
+ | 'uk'
246
+ | 'ur'
247
+ | 'uz'
248
+ | 've'
249
+ | 'vi'
250
+ | 'vo'
251
+ | 'wa'
252
+ | 'wo'
253
+ | 'xh'
254
+ | 'yi'
255
+ | 'yo'
256
+ | 'za'
257
+ | 'zh'
258
+ | 'zu';
259
+
55
260
  export declare interface Lookup<ID = string> {
56
261
  id: ID;
57
262
  name: string;
@@ -76,23 +281,28 @@ export declare interface Options {
76
281
  apiVersion?: string;
77
282
  }
78
283
 
284
+ declare type Pagination = {
285
+ take?: number;
286
+ skip?: number;
287
+ };
288
+
79
289
  export declare interface Project {
80
290
  id: string;
81
291
  name: string;
82
- languages: string[];
292
+ languages: LanguageCode[];
83
293
  contentTypes?: ContentType[];
84
294
  }
85
295
 
86
- declare type Query<M extends Model = Model> = {
87
- id?: ClientFilter;
88
- lastUpdate?: ClientFilter;
89
- language?: ClientFilter<M['language'] extends undefined ? string : M['language']>;
90
- contentType?: ClientFilter;
91
- includeRelatedContent?: boolean;
92
- } & Partial<Omit<{
93
- [K in keyof M as `fields.${string & K}`]: ClientFilter;
94
- }, 'fields.id' | 'fields.language'>>;
296
+ declare type QueryParams<M extends Model = Model & Record<string, any>> = FilterableFields<M> & {
297
+ sort?: SortableFields<M>;
298
+ pagination?: Pagination;
299
+ };
300
+
301
+ declare type SortableFields<M extends Model> = {
302
+ contentType?: SortOrder;
303
+ lastUpdate?: SortOrder;
304
+ } & AllowedModelFields<M, SortOrder>;
95
305
 
96
- export declare type QueryParams<M extends Model = Model & Record<string, any>> = Query<M>;
306
+ declare type SortOrder = 'asc' | 'desc';
97
307
 
98
308
  export { }
package/dist/index.js CHANGED
@@ -1,42 +1,55 @@
1
- const a = {
1
+ const N = {
2
2
  IS_PRODUCTION: !0,
3
3
  DEFAULT_API_CLIENT_DOMAIN: "api.contentisland.net",
4
4
  DEFAULT_API_CLIENT_VERSION: "1.0"
5
- }, u = {
5
+ }, $ = {
6
6
  SESSION_KEY: "authorization",
7
7
  SESSION_TYPE: "Bearer"
8
- }, O = (t) => t == null ? void 0 : t.includes("|"), S = (t, e) => Array.isArray(t) ? t.map(e) : [], C = (t, e) => t.isArray ? S(t.value, (o) => c(o, e)) : c(t.value, e), I = (t) => O(t.type) && (t.isArray ? t.value.every((e) => typeof e == "object" && "contentType" in e) : typeof t.value == "object" && "contentType" in t.value), _ = (t, e) => t.reduce(
8
+ }, g = (t) => t == null ? void 0 : t.includes("|"), l = (t, e) => Array.isArray(t) ? t.map(e) : [], I = (t, e) => t.isArray ? l(t.value, (o) => T(o, e)) : T(t.value, e), O = (t) => g(t.type) && (t.isArray ? t.value.every((e) => typeof e == "object" && "contentType" in e) : typeof t.value == "object" && "contentType" in t.value), _ = (t, e) => t.reduce(
9
9
  (o, n) => ({
10
10
  ...o,
11
- [n.name]: I(n) ? C(n, e) : n.value
11
+ [n.name]: O(n) ? I(n, e) : n.value
12
12
  }),
13
13
  {}
14
- ), A = (t, e, o) => {
15
- var l;
16
- const n = o ?? ((l = t[0]) == null ? void 0 : l.language), r = t.filter((g) => g.language === n), s = _(r, n);
17
- return Object.keys(s).length === 0 && console.warn(`No fields found for language "${n}" and content id "${e}"`), {
14
+ ), C = (t, e, o) => {
15
+ var s;
16
+ const n = o ?? ((s = t[0]) == null ? void 0 : s.language), r = t.filter((E) => E.language === n), c = _(r, n);
17
+ return Object.keys(c).length === 0 && console.warn(`No fields found for language "${n}" and content id "${e}"`), {
18
18
  id: e,
19
19
  language: n,
20
- ...s
20
+ ...c
21
21
  };
22
- }, c = (t, e) => Array.isArray(t == null ? void 0 : t.fields) ? A(t.fields, t.id, e) : {}, N = (t, e) => {
22
+ }, T = (t, e) => Array.isArray(t == null ? void 0 : t.fields) ? C(t.fields, t.id, e) : {}, u = (t, e) => {
23
23
  if (typeof e == "string" || typeof e == "boolean")
24
24
  return `${t}=${e}`;
25
25
  if (e.in)
26
26
  return `${t}[in]=${e.in.join(",")}`;
27
- }, $ = (t, e) => Object.entries(t).reduce(
28
- (o, [n, r], s) => s === 0 ? N(n, r) : `${o}${e}${N(n, r)}`,
29
- ""
30
- ), E = (t) => t && Object.keys(t).length > 0 ? `?${$(t, "&")}` : "", T = {
27
+ }, p = (t) => Object.entries(t).map(([e, o]) => `${e}:${o}`).join(","), A = (t, e) => {
28
+ let o = "";
29
+ return t.take !== void 0 && (o += `take=${t.take}`), t.skip !== void 0 && (o += o ? `${e}skip=${t.skip}` : `skip=${t.skip}`), o;
30
+ }, L = (t, e) => Object.entries(t).reduce((o, [n, r], c) => {
31
+ if (n === "sort" && typeof r == "object" && r !== null && !Array.isArray(r)) {
32
+ const s = p(r);
33
+ return c === 0 ? `sort=${s}` : `${o}${e}sort=${s}`;
34
+ } else if (n === "pagination" && typeof r == "object" && r !== null) {
35
+ const s = A(r, e);
36
+ return c === 0 ? s : `${o}${e}${s}`;
37
+ } else if (r !== void 0 && n !== "sort" && n !== "pagination") {
38
+ const s = r;
39
+ return c === 0 ? u(n, s) : `${o}${e}${u(n, s)}`;
40
+ }
41
+ return o;
42
+ }, ""), S = (t) => t && Object.keys(t).length > 0 ? `?${L(t, "&")}` : "", i = {
31
43
  PROJECT: "/project",
32
- CONTENT_LIST: (t) => `/contents${E(t)}`,
33
- CONTENT: (t) => `/content${E(t)}`
44
+ CONTENT_LIST: (t) => `/contents${S(t)}`,
45
+ CONTENT: (t) => `/content${S(t)}`,
46
+ CONTENT_LIST_SIZE: (t) => t ? `/contents/size${S(t)}` : "/contents/size"
34
47
  }, d = (t) => {
35
- const o = (t.secureProtocol === void 0 ? a.IS_PRODUCTION : t.secureProtocol) ? "https" : "http", n = t.domain ? t.domain : a.DEFAULT_API_CLIENT_DOMAIN, r = t.apiVersion ? t.apiVersion : a.DEFAULT_API_CLIENT_VERSION;
48
+ const o = (t.secureProtocol === void 0 ? N.IS_PRODUCTION : t.secureProtocol) ? "https" : "http", n = t.domain ? t.domain : N.DEFAULT_API_CLIENT_DOMAIN, r = t.apiVersion ? t.apiVersion : N.DEFAULT_API_CLIENT_VERSION;
36
49
  return `${o}://${n}/api/${r}`;
37
- }, i = async (t, e) => fetch(`${d(e)}${t}`, {
50
+ }, a = async (t, e) => fetch(`${d(e)}${t}`, {
38
51
  headers: {
39
- [u.SESSION_KEY]: `${u.SESSION_TYPE} ${e.accessToken}`
52
+ [$.SESSION_KEY]: `${$.SESSION_TYPE} ${e.accessToken}`
40
53
  }
41
54
  }).then((o) => {
42
55
  if (o.ok)
@@ -47,23 +60,24 @@ const a = {
47
60
  statusText: o.statusText
48
61
  })
49
62
  );
50
- }), p = (t) => {
51
- const e = (n) => i(T.CONTENT_LIST(n), t), o = (n) => i(T.CONTENT(n), t);
63
+ }), f = (t) => {
64
+ const e = (n) => a(i.CONTENT_LIST(n), t), o = (n) => a(i.CONTENT(n), t);
52
65
  return {
53
- getProject: () => i(T.PROJECT, t),
66
+ getProject: () => a(i.PROJECT, t),
54
67
  getContentList: (n) => e(n).then(
55
68
  (r) => Array.isArray(r) ? r.map(
56
- (s) => c(s, n == null ? void 0 : n.language)
69
+ (c) => T(c, n == null ? void 0 : n.language)
57
70
  ) : []
58
71
  ),
59
72
  getContent: (n) => o(n).then(
60
- (r) => c(r, n == null ? void 0 : n.language)
73
+ (r) => T(r, n == null ? void 0 : n.language)
61
74
  ),
62
75
  getRawContentList: e,
63
- getRawContent: o
76
+ getRawContent: o,
77
+ getContentListSize: (n) => a(i.CONTENT_LIST_SIZE(n), t)
64
78
  };
65
79
  };
66
80
  export {
67
- p as createClient,
68
- c as mapContentToModel
81
+ f as createClient,
82
+ T as mapContentToModel
69
83
  };
@@ -1 +1 @@
1
- (function(r,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(r=typeof globalThis<"u"?globalThis:r||self,c(r.ApiClient={}))})(this,function(r){"use strict";const c={IS_PRODUCTION:!0,DEFAULT_API_CLIENT_DOMAIN:"api.contentisland.net",DEFAULT_API_CLIENT_VERSION:"1.0"},u={SESSION_KEY:"authorization",SESSION_TYPE:"Bearer"},g=e=>e==null?void 0:e.includes("|"),C=(e,t)=>Array.isArray(e)?e.map(t):[],S=(e,t)=>e.isArray?C(e.value,o=>i(o,t)):i(e.value,t),O=e=>g(e.type)&&(e.isArray?e.value.every(t=>typeof t=="object"&&"contentType"in t):typeof e.value=="object"&&"contentType"in e.value),A=(e,t)=>e.reduce((o,n)=>({...o,[n.name]:O(n)?S(n,t):n.value}),{}),I=(e,t,o)=>{var E;const n=o??((E=e[0])==null?void 0:E.language),s=e.filter(f=>f.language===n),a=A(s,n);return Object.keys(a).length===0&&console.warn(`No fields found for language "${n}" and content id "${t}"`),{id:t,language:n,...a}},i=(e,t)=>Array.isArray(e==null?void 0:e.fields)?I(e.fields,e.id,t):{},N=(e,t)=>{if(typeof t=="string"||typeof t=="boolean")return`${e}=${t}`;if(t.in)return`${e}[in]=${t.in.join(",")}`},_=(e,t)=>Object.entries(e).reduce((o,[n,s],a)=>a===0?N(n,s):`${o}${t}${N(n,s)}`,""),d=e=>e&&Object.keys(e).length>0?`?${_(e,"&")}`:"",T={PROJECT:"/project",CONTENT_LIST:e=>`/contents${d(e)}`,CONTENT:e=>`/content${d(e)}`},p=e=>{const o=(e.secureProtocol===void 0?c.IS_PRODUCTION:e.secureProtocol)?"https":"http",n=e.domain?e.domain:c.DEFAULT_API_CLIENT_DOMAIN,s=e.apiVersion?e.apiVersion:c.DEFAULT_API_CLIENT_VERSION;return`${o}://${n}/api/${s}`},l=async(e,t)=>fetch(`${p(t)}${e}`,{headers:{[u.SESSION_KEY]:`${u.SESSION_TYPE} ${t.accessToken}`}}).then(o=>{if(o.ok)return o.json();throw Error(JSON.stringify({status:o.status,statusText:o.statusText}))}),$=e=>{const t=n=>l(T.CONTENT_LIST(n),e),o=n=>l(T.CONTENT(n),e);return{getProject:()=>l(T.PROJECT,e),getContentList:n=>t(n).then(s=>Array.isArray(s)?s.map(a=>i(a,n==null?void 0:n.language)):[]),getContent:n=>o(n).then(s=>i(s,n==null?void 0:n.language)),getRawContentList:t,getRawContent:o}};r.createClient=$,r.mapContentToModel=i,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
1
+ (function(c,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(c=typeof globalThis<"u"?globalThis:c||self,a(c.ApiClient={}))})(this,function(c){"use strict";const a={IS_PRODUCTION:!0,DEFAULT_API_CLIENT_DOMAIN:"api.contentisland.net",DEFAULT_API_CLIENT_VERSION:"1.0"},N={SESSION_KEY:"authorization",SESSION_TYPE:"Bearer"},E=t=>t==null?void 0:t.includes("|"),g=(t,e)=>Array.isArray(t)?t.map(e):[],p=(t,e)=>t.isArray?g(t.value,o=>T(o,e)):T(t.value,e),d=t=>E(t.type)&&(t.isArray?t.value.every(e=>typeof e=="object"&&"contentType"in e):typeof t.value=="object"&&"contentType"in t.value),C=(t,e)=>t.reduce((o,n)=>({...o,[n.name]:d(n)?p(n,e):n.value}),{}),O=(t,e,o)=>{var s;const n=o??((s=t[0])==null?void 0:s.language),r=t.filter(h=>h.language===n),i=C(r,n);return Object.keys(i).length===0&&console.warn(`No fields found for language "${n}" and content id "${e}"`),{id:e,language:n,...i}},T=(t,e)=>Array.isArray(t==null?void 0:t.fields)?O(t.fields,t.id,e):{},$=(t,e)=>{if(typeof e=="string"||typeof e=="boolean")return`${t}=${e}`;if(e.in)return`${t}[in]=${e.in.join(",")}`},I=t=>Object.entries(t).map(([e,o])=>`${e}:${o}`).join(","),_=(t,e)=>{let o="";return t.take!==void 0&&(o+=`take=${t.take}`),t.skip!==void 0&&(o+=o?`${e}skip=${t.skip}`:`skip=${t.skip}`),o},f=(t,e)=>Object.entries(t).reduce((o,[n,r],i)=>{if(n==="sort"&&typeof r=="object"&&r!==null&&!Array.isArray(r)){const s=I(r);return i===0?`sort=${s}`:`${o}${e}sort=${s}`}else if(n==="pagination"&&typeof r=="object"&&r!==null){const s=_(r,e);return i===0?s:`${o}${e}${s}`}else if(r!==void 0&&n!=="sort"&&n!=="pagination"){const s=r;return i===0?$(n,s):`${o}${e}${$(n,s)}`}return o},""),S=t=>t&&Object.keys(t).length>0?`?${f(t,"&")}`:"",u={PROJECT:"/project",CONTENT_LIST:t=>`/contents${S(t)}`,CONTENT:t=>`/content${S(t)}`,CONTENT_LIST_SIZE:t=>t?`/contents/size${S(t)}`:"/contents/size"},A=t=>{const o=(t.secureProtocol===void 0?a.IS_PRODUCTION:t.secureProtocol)?"https":"http",n=t.domain?t.domain:a.DEFAULT_API_CLIENT_DOMAIN,r=t.apiVersion?t.apiVersion:a.DEFAULT_API_CLIENT_VERSION;return`${o}://${n}/api/${r}`},l=async(t,e)=>fetch(`${A(e)}${t}`,{headers:{[N.SESSION_KEY]:`${N.SESSION_TYPE} ${e.accessToken}`}}).then(o=>{if(o.ok)return o.json();throw Error(JSON.stringify({status:o.status,statusText:o.statusText}))}),L=t=>{const e=n=>l(u.CONTENT_LIST(n),t),o=n=>l(u.CONTENT(n),t);return{getProject:()=>l(u.PROJECT,t),getContentList:n=>e(n).then(r=>Array.isArray(r)?r.map(i=>T(i,n==null?void 0:n.language)):[]),getContent:n=>o(n).then(r=>T(r,n==null?void 0:n.language)),getRawContentList:e,getRawContent:o,getContentListSize:n=>l(u.CONTENT_LIST_SIZE(n),t)}};c.createClient=L,c.mapContentToModel=T,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@content-island/api-client",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "Content Island - REST API Client",
5
5
  "private": false,
6
6
  "sideEffects": false,