@content-island/api-client 0.15.0 → 0.17.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 +53 -15
- package/dist/index.js +48 -47
- package/dist/index.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
declare type
|
|
2
|
-
[K in keyof M as M[K] extends
|
|
1
|
+
declare type AllowedFields<M extends Model, Type extends 'sort' | 'filter'> = Partial<Omit<{
|
|
2
|
+
[K in keyof M as IsPrimitive<NonNullable<M[K]>> extends true ? `fields.${string & K}` : never]?: Type extends 'sort' ? SortOrder : ClientFilter<NonNullable<M[K]>>;
|
|
3
3
|
}, 'fields.id' | 'fields.language'>>;
|
|
4
4
|
|
|
5
5
|
export declare interface ApiClient {
|
|
@@ -12,7 +12,13 @@ export declare interface ApiClient {
|
|
|
12
12
|
updateContentFieldValue: (contentId: string, fieldId: string, value: any) => Promise<boolean>;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
declare interface BaseModel {
|
|
16
|
+
id: string;
|
|
17
|
+
name: string;
|
|
18
|
+
type: ModelType;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export declare type ClientFilter<Type = string | boolean | number> = Type | {
|
|
16
22
|
in?: Type[];
|
|
17
23
|
ne?: Type;
|
|
18
24
|
};
|
|
@@ -31,10 +37,6 @@ export declare type ContentListSizeQueryParams<M extends Model = Model & Record<
|
|
|
31
37
|
|
|
32
38
|
export declare type ContentQueryParams<M extends Model = Model & Record<string, any>> = Omit<QueryParams<M>, 'sort' | 'pagination'>;
|
|
33
39
|
|
|
34
|
-
declare interface ContentType extends Lookup {
|
|
35
|
-
fields: ContentTypeField[];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
40
|
declare interface ContentTypeField extends Lookup {
|
|
39
41
|
type: FieldType;
|
|
40
42
|
tsType: string;
|
|
@@ -44,6 +46,27 @@ declare interface ContentTypeField extends Lookup {
|
|
|
44
46
|
|
|
45
47
|
export declare const createClient: (options: Options) => ApiClient;
|
|
46
48
|
|
|
49
|
+
declare interface Entity extends BaseModel {
|
|
50
|
+
type: 'entity';
|
|
51
|
+
fieldList?: Field_2[];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
declare type EntityContentType = Omit<Entity, 'fieldList'> & {
|
|
55
|
+
fields: ContentTypeField[];
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
declare interface Enum extends BaseModel {
|
|
59
|
+
type: 'enum';
|
|
60
|
+
values: EnumValue[];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
declare type EnumContentType = Enum;
|
|
64
|
+
|
|
65
|
+
declare interface EnumValue {
|
|
66
|
+
id: string;
|
|
67
|
+
value: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
47
70
|
export declare interface Field {
|
|
48
71
|
id: string;
|
|
49
72
|
name: string;
|
|
@@ -53,7 +76,14 @@ export declare interface Field {
|
|
|
53
76
|
language: string;
|
|
54
77
|
}
|
|
55
78
|
|
|
56
|
-
declare
|
|
79
|
+
declare interface Field_2 {
|
|
80
|
+
id: string;
|
|
81
|
+
name: string;
|
|
82
|
+
type: FieldType;
|
|
83
|
+
isArray: boolean;
|
|
84
|
+
order: number;
|
|
85
|
+
validations?: Validation[];
|
|
86
|
+
}
|
|
57
87
|
|
|
58
88
|
export declare type FieldType =
|
|
59
89
|
| 'short-text'
|
|
@@ -64,15 +94,17 @@ export declare type FieldType =
|
|
|
64
94
|
| 'media'
|
|
65
95
|
| 'boolean'
|
|
66
96
|
| 'color'
|
|
67
|
-
|
|
|
97
|
+
| RelatedModelType;
|
|
68
98
|
|
|
69
99
|
declare type FilterableFields<M extends Model = Model> = {
|
|
70
|
-
id?: ClientFilter
|
|
71
|
-
lastUpdate?: ClientFilter
|
|
100
|
+
id?: ClientFilter<string>;
|
|
101
|
+
lastUpdate?: ClientFilter<string>;
|
|
72
102
|
language?: M['language'];
|
|
73
|
-
contentType?: ClientFilter
|
|
103
|
+
contentType?: ClientFilter<string>;
|
|
74
104
|
includeRelatedContent?: boolean;
|
|
75
|
-
} &
|
|
105
|
+
} & AllowedFields<M, 'filter'>;
|
|
106
|
+
|
|
107
|
+
declare type IsPrimitive<T> = [T] extends [string | number | boolean] ? true : false;
|
|
76
108
|
|
|
77
109
|
export declare type LanguageCode =
|
|
78
110
|
| 'aa'
|
|
@@ -278,6 +310,8 @@ export declare type Model<Language = string> = {
|
|
|
278
310
|
lastUpdate?: string;
|
|
279
311
|
};
|
|
280
312
|
|
|
313
|
+
declare type ModelType = 'entity' | 'enum';
|
|
314
|
+
|
|
281
315
|
export declare interface Options {
|
|
282
316
|
accessToken: string;
|
|
283
317
|
domain?: string;
|
|
@@ -295,7 +329,7 @@ export declare interface Project {
|
|
|
295
329
|
id: string;
|
|
296
330
|
name: string;
|
|
297
331
|
languages: LanguageCode[];
|
|
298
|
-
contentTypes?:
|
|
332
|
+
contentTypes?: (EntityContentType | EnumContentType)[];
|
|
299
333
|
}
|
|
300
334
|
|
|
301
335
|
declare type QueryParams<M extends Model = Model & Record<string, any>> = FilterableFields<M> & {
|
|
@@ -303,11 +337,15 @@ declare type QueryParams<M extends Model = Model & Record<string, any>> = Filter
|
|
|
303
337
|
pagination?: Pagination;
|
|
304
338
|
};
|
|
305
339
|
|
|
340
|
+
declare type RelatedModelType = `${string}|${string}`;
|
|
341
|
+
|
|
306
342
|
declare type SortableFields<M extends Model> = {
|
|
307
343
|
contentType?: SortOrder;
|
|
308
344
|
lastUpdate?: SortOrder;
|
|
309
|
-
} &
|
|
345
|
+
} & AllowedFields<M, 'sort'>;
|
|
310
346
|
|
|
311
347
|
declare type SortOrder = 'asc' | 'desc';
|
|
312
348
|
|
|
349
|
+
declare type Validation = { name: string; customArgs?: any };
|
|
350
|
+
|
|
313
351
|
export { }
|
package/dist/index.js
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
|
-
const N = (t, e) => {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
const l = (t, e) => `&${t}.type=${typeof e}`, A = (t) => typeof t == "string" || typeof t == "number" || typeof t == "boolean", O = (t, e, n) => `${t}=${e}${n ? l(t, e) : ""}`, N = (t, e) => {
|
|
2
|
+
const n = t.startsWith("fields.");
|
|
3
|
+
if (A(e))
|
|
4
|
+
return O(t, e, n);
|
|
4
5
|
if (e.in)
|
|
5
|
-
return `${t}[in]=${e.in.join(",")}`;
|
|
6
|
-
if (e.ne)
|
|
7
|
-
return `${t}[ne]=${e.ne}`;
|
|
8
|
-
},
|
|
6
|
+
return `${t}[in]=${e.in.join(",")}${n && e.in.length > 0 ? l(t, e.in[0]) : ""}`;
|
|
7
|
+
if (e.ne !== void 0)
|
|
8
|
+
return `${t}[ne]=${e.ne}${n ? l(t, e.ne) : ""}`;
|
|
9
|
+
}, _ = (t) => Object.entries(t).map(([e, n]) => `${e}:${n}`).join(","), p = (t, e) => {
|
|
9
10
|
let n = "";
|
|
10
11
|
return t.take !== void 0 && (n += `take=${t.take}`), t.skip !== void 0 && (n += n ? `${e}skip=${t.skip}` : `skip=${t.skip}`), n;
|
|
11
|
-
},
|
|
12
|
-
if (o === "sort" && typeof
|
|
13
|
-
const
|
|
14
|
-
return a === 0 ? `sort=${
|
|
15
|
-
} else if (o === "pagination" && typeof
|
|
16
|
-
const
|
|
17
|
-
return a === 0 ?
|
|
18
|
-
} else if (
|
|
19
|
-
const
|
|
20
|
-
return a === 0 ? N(o,
|
|
12
|
+
}, C = (t, e) => Object.entries(t).reduce((n, [o, r], a) => {
|
|
13
|
+
if (o === "sort" && typeof r == "object" && r !== null && !Array.isArray(r)) {
|
|
14
|
+
const s = _(r);
|
|
15
|
+
return a === 0 ? `sort=${s}` : `${n}${e}sort=${s}`;
|
|
16
|
+
} else if (o === "pagination" && typeof r == "object" && r !== null) {
|
|
17
|
+
const s = p(r, e);
|
|
18
|
+
return a === 0 ? s : `${n}${e}${s}`;
|
|
19
|
+
} else if (r !== void 0 && o !== "sort" && o !== "pagination") {
|
|
20
|
+
const s = r;
|
|
21
|
+
return a === 0 ? N(o, s) : `${n}${e}${N(o, s)}`;
|
|
21
22
|
}
|
|
22
23
|
return n;
|
|
23
|
-
}, ""),
|
|
24
|
+
}, ""), g = (t) => t && Object.keys(t).length > 0 ? `?${C(t, "&")}` : "", S = {
|
|
24
25
|
IS_PRODUCTION: !0,
|
|
25
26
|
DEFAULT_API_CLIENT_DOMAIN: "api.contentisland.net",
|
|
26
27
|
DEFAULT_API_CLIENT_VERSION: "1.0"
|
|
@@ -28,10 +29,10 @@ const N = (t, e) => {
|
|
|
28
29
|
SESSION_KEY: "authorization",
|
|
29
30
|
SESSION_TYPE: "Bearer",
|
|
30
31
|
METADATA_KEY: "x-metadata"
|
|
31
|
-
},
|
|
32
|
-
const n = (t.secureProtocol === void 0 ?
|
|
33
|
-
return `${n}://${o}/api/${
|
|
34
|
-
},
|
|
32
|
+
}, d = (t) => {
|
|
33
|
+
const n = (t.secureProtocol === void 0 ? S.IS_PRODUCTION : t.secureProtocol) ? "https" : "http", o = t.domain ? t.domain : S.DEFAULT_API_CLIENT_DOMAIN, r = t.apiVersion ? t.apiVersion : S.DEFAULT_API_CLIENT_VERSION;
|
|
34
|
+
return `${n}://${o}/api/${r}`;
|
|
35
|
+
}, I = async (t, e) => fetch(`${d(e)}${t}`, {
|
|
35
36
|
headers: {
|
|
36
37
|
[i.SESSION_KEY]: `${i.SESSION_TYPE} ${e.accessToken}`,
|
|
37
38
|
...e.metadata ? { [i.METADATA_KEY]: e.metadata } : {}
|
|
@@ -45,7 +46,7 @@ const N = (t, e) => {
|
|
|
45
46
|
statusText: n.statusText
|
|
46
47
|
})
|
|
47
48
|
);
|
|
48
|
-
}),
|
|
49
|
+
}), L = async (t, e, n) => fetch(`${d(n)}${t}`, {
|
|
49
50
|
method: "PUT",
|
|
50
51
|
headers: {
|
|
51
52
|
"Content-Type": "application/json",
|
|
@@ -63,51 +64,51 @@ const N = (t, e) => {
|
|
|
63
64
|
})
|
|
64
65
|
);
|
|
65
66
|
}), T = {
|
|
66
|
-
get:
|
|
67
|
-
put:
|
|
68
|
-
},
|
|
67
|
+
get: I,
|
|
68
|
+
put: L
|
|
69
|
+
}, h = (t) => t == null ? void 0 : t.includes("|"), f = (t, e) => Array.isArray(t) ? t.map(e) : [], y = (t, e) => t.isArray ? f(t.value, (n) => E(n, e)) : E(t.value, e), F = (t) => h(t.type) && (t.isArray ? t.value.every((e) => typeof e == "object" && "contentType" in e) : typeof t.value == "object" && "contentType" in t.value), j = (t, e) => t.reduce(
|
|
69
70
|
(n, o) => ({
|
|
70
71
|
...n,
|
|
71
|
-
[o.name]:
|
|
72
|
+
[o.name]: F(o) ? y(o, e) : o.value
|
|
72
73
|
}),
|
|
73
74
|
{}
|
|
74
|
-
),
|
|
75
|
+
), R = (t, e, n, o) => {
|
|
75
76
|
var c;
|
|
76
|
-
const
|
|
77
|
-
return Object.keys(
|
|
77
|
+
const r = n ?? ((c = t[0]) == null ? void 0 : c.language), a = t.filter(($) => $.language === r), s = j(a, r);
|
|
78
|
+
return Object.keys(s).length === 0 && console.warn(`No fields found for language "${r}" and content id "${e}"`), {
|
|
78
79
|
id: e,
|
|
79
|
-
language:
|
|
80
|
+
language: r,
|
|
80
81
|
lastUpdate: o,
|
|
81
|
-
...
|
|
82
|
+
...s
|
|
82
83
|
};
|
|
83
|
-
}, E = (t, e) => Array.isArray(t == null ? void 0 : t.fields) ?
|
|
84
|
+
}, E = (t, e) => Array.isArray(t == null ? void 0 : t.fields) ? R(t.fields, t.id, e, t.lastUpdate) : {}, u = {
|
|
84
85
|
PROJECT: "/project",
|
|
85
|
-
CONTENT_LIST: (t) => `/contents${
|
|
86
|
-
CONTENT: (t) => `/content${
|
|
87
|
-
CONTENT_LIST_SIZE: (t) => t ? `/contents/size${
|
|
88
|
-
},
|
|
89
|
-
const e = (a) => T.get(u.CONTENT_LIST(a), t), n = (a) => T.get(u.CONTENT(a), t), o = () => T.get(u.PROJECT, t),
|
|
90
|
-
var
|
|
86
|
+
CONTENT_LIST: (t) => `/contents${g(t)}`,
|
|
87
|
+
CONTENT: (t) => `/content${g(t)}`,
|
|
88
|
+
CONTENT_LIST_SIZE: (t) => t ? `/contents/size${g(t)}` : "/contents/size"
|
|
89
|
+
}, D = (t) => {
|
|
90
|
+
const e = (a) => T.get(u.CONTENT_LIST(a), t), n = (a) => T.get(u.CONTENT(a), t), o = () => T.get(u.PROJECT, t), r = async () => {
|
|
91
|
+
var s;
|
|
91
92
|
const a = await o();
|
|
92
|
-
return (
|
|
93
|
+
return (s = a == null ? void 0 : a.languages) == null ? void 0 : s[0];
|
|
93
94
|
};
|
|
94
95
|
return {
|
|
95
96
|
getProject: o,
|
|
96
97
|
getContentList: async (a) => {
|
|
97
|
-
const
|
|
98
|
-
return Array.isArray(
|
|
98
|
+
const s = await e(a), c = (a == null ? void 0 : a.language) ?? await r();
|
|
99
|
+
return Array.isArray(s) ? s.map(($) => E($, c)) : [];
|
|
99
100
|
},
|
|
100
101
|
getContent: async (a) => {
|
|
101
|
-
const
|
|
102
|
-
return E(
|
|
102
|
+
const s = await n(a), c = (a == null ? void 0 : a.language) ?? await r();
|
|
103
|
+
return E(s, c);
|
|
103
104
|
},
|
|
104
105
|
getRawContentList: e,
|
|
105
106
|
getRawContent: n,
|
|
106
107
|
getContentListSize: (a) => T.get(u.CONTENT_LIST_SIZE(a), t),
|
|
107
|
-
updateContentFieldValue: (a,
|
|
108
|
+
updateContentFieldValue: (a, s, c) => T.put(
|
|
108
109
|
`${u.CONTENT(null)}/${a}`,
|
|
109
110
|
{
|
|
110
|
-
id:
|
|
111
|
+
id: s,
|
|
111
112
|
value: c
|
|
112
113
|
},
|
|
113
114
|
t
|
|
@@ -115,6 +116,6 @@ const N = (t, e) => {
|
|
|
115
116
|
};
|
|
116
117
|
};
|
|
117
118
|
export {
|
|
118
|
-
|
|
119
|
+
D as createClient,
|
|
119
120
|
E as mapContentToModel
|
|
120
121
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(i,
|
|
1
|
+
(function(i,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(i=typeof globalThis<"u"?globalThis:i||self,u(i.ApiClient={}))})(this,function(i){"use strict";const u=(t,e)=>`&${t}.type=${typeof e}`,A=t=>typeof t=="string"||typeof t=="number"||typeof t=="boolean",O=(t,e,n)=>`${t}=${e}${n?u(t,e):""}`,N=(t,e)=>{const n=t.startsWith("fields.");if(A(e))return O(t,e,n);if(e.in)return`${t}[in]=${e.in.join(",")}${n&&e.in.length>0?u(t,e.in[0]):""}`;if(e.ne!==void 0)return`${t}[ne]=${e.ne}${n?u(t,e.ne):""}`},C=t=>Object.entries(t).map(([e,n])=>`${e}:${n}`).join(","),_=(t,e)=>{let n="";return t.take!==void 0&&(n+=`take=${t.take}`),t.skip!==void 0&&(n+=n?`${e}skip=${t.skip}`:`skip=${t.skip}`),n},I=(t,e)=>Object.entries(t).reduce((n,[s,r],o)=>{if(s==="sort"&&typeof r=="object"&&r!==null&&!Array.isArray(r)){const a=C(r);return o===0?`sort=${a}`:`${n}${e}sort=${a}`}else if(s==="pagination"&&typeof r=="object"&&r!==null){const a=_(r,e);return o===0?a:`${n}${e}${a}`}else if(r!==void 0&&s!=="sort"&&s!=="pagination"){const a=r;return o===0?N(s,a):`${n}${e}${N(s,a)}`}return n},""),d=t=>t&&Object.keys(t).length>0?`?${I(t,"&")}`:"",$={IS_PRODUCTION:!0,DEFAULT_API_CLIENT_DOMAIN:"api.contentisland.net",DEFAULT_API_CLIENT_VERSION:"1.0"},T={SESSION_KEY:"authorization",SESSION_TYPE:"Bearer",METADATA_KEY:"x-metadata"},p=t=>{const n=(t.secureProtocol===void 0?$.IS_PRODUCTION:t.secureProtocol)?"https":"http",s=t.domain?t.domain:$.DEFAULT_API_CLIENT_DOMAIN,r=t.apiVersion?t.apiVersion:$.DEFAULT_API_CLIENT_VERSION;return`${n}://${s}/api/${r}`},g={get:async(t,e)=>fetch(`${p(e)}${t}`,{headers:{[T.SESSION_KEY]:`${T.SESSION_TYPE} ${e.accessToken}`,...e.metadata?{[T.METADATA_KEY]:e.metadata}:{}}}).then(n=>{if(n.ok)return n.json();throw Error(JSON.stringify({status:n.status,statusText:n.statusText}))}),put:async(t,e,n)=>fetch(`${p(n)}${t}`,{method:"PUT",headers:{"Content-Type":"application/json",[T.SESSION_KEY]:`${T.SESSION_TYPE} ${n.accessToken}`,...n.metadata?{[T.METADATA_KEY]:n.metadata}:{}},body:JSON.stringify(e)}).then(s=>{if(s.ok)return!0;throw Error(JSON.stringify({status:s.status,statusText:s.statusText}))})},f=t=>t==null?void 0:t.includes("|"),h=(t,e)=>Array.isArray(t)?t.map(e):[],y=(t,e)=>t.isArray?h(t.value,n=>l(n,e)):l(t.value,e),L=t=>f(t.type)&&(t.isArray?t.value.every(e=>typeof e=="object"&&"contentType"in e):typeof t.value=="object"&&"contentType"in t.value),j=(t,e)=>t.reduce((n,s)=>({...n,[s.name]:L(s)?y(s,e):s.value}),{}),F=(t,e,n,s)=>{var c;const r=n??((c=t[0])==null?void 0:c.language),o=t.filter(S=>S.language===r),a=j(o,r);return Object.keys(a).length===0&&console.warn(`No fields found for language "${r}" and content id "${e}"`),{id:e,language:r,lastUpdate:s,...a}},l=(t,e)=>Array.isArray(t==null?void 0:t.fields)?F(t.fields,t.id,e,t.lastUpdate):{},E={PROJECT:"/project",CONTENT_LIST:t=>`/contents${d(t)}`,CONTENT:t=>`/content${d(t)}`,CONTENT_LIST_SIZE:t=>t?`/contents/size${d(t)}`:"/contents/size"},R=t=>{const e=o=>g.get(E.CONTENT_LIST(o),t),n=o=>g.get(E.CONTENT(o),t),s=()=>g.get(E.PROJECT,t),r=async()=>{var a;const o=await s();return(a=o==null?void 0:o.languages)==null?void 0:a[0]};return{getProject:s,getContentList:async o=>{const a=await e(o),c=(o==null?void 0:o.language)??await r();return Array.isArray(a)?a.map(S=>l(S,c)):[]},getContent:async o=>{const a=await n(o),c=(o==null?void 0:o.language)??await r();return l(a,c)},getRawContentList:e,getRawContent:n,getContentListSize:o=>g.get(E.CONTENT_LIST_SIZE(o),t),updateContentFieldValue:(o,a,c)=>g.put(`${E.CONTENT(null)}/${o}`,{id:a,value:c},t)}};i.createClient=R,i.mapContentToModel=l,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
|