@content-island/api-client 0.7.0 → 0.8.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 +31 -32
- package/dist/index.js +48 -39
- package/dist/index.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
declare type AllowedQueryKeys = keyof Pick<
|
|
1
|
+
declare type AllowedQueryKeys<M extends Model = Model> = keyof Pick<
|
|
2
|
+
Query<M>,
|
|
3
|
+
'id' | 'contentType' | 'language' | Extract<keyof Query<M>, `fields.${string}`>
|
|
4
|
+
>;
|
|
2
5
|
|
|
3
6
|
export declare interface ApiClient {
|
|
4
7
|
getProject: () => Promise<Project>;
|
|
5
|
-
getContentList: (queryParam?: QueryParams) => Promise<
|
|
6
|
-
getContent:
|
|
8
|
+
getContentList: <M extends Model = Model & Record<string, any>>(queryParam?: QueryParams<M>) => Promise<M[]>;
|
|
9
|
+
getContent: <M extends Model = Model & Record<string, any>>(queryParam: QueryParams<M>) => Promise<M>;
|
|
10
|
+
getRawContentList: <M extends Model = Model & Record<string, any>>(queryParam?: QueryParams<M>) => Promise<Content[]>;
|
|
11
|
+
getRawContent: <M extends Model = Model & Record<string, any>>(queryParam: QueryParams<M>) => Promise<Content>;
|
|
7
12
|
}
|
|
8
13
|
|
|
9
14
|
export declare interface ApiLookup {
|
|
@@ -11,12 +16,7 @@ export declare interface ApiLookup {
|
|
|
11
16
|
name: string;
|
|
12
17
|
}
|
|
13
18
|
|
|
14
|
-
declare type
|
|
15
|
-
id: string;
|
|
16
|
-
language?: Language;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export declare type ClientFilter = string | { in?: string[] };
|
|
19
|
+
export declare type ClientFilter<Type = string> = Type | { in?: Type[] };
|
|
20
20
|
|
|
21
21
|
export declare interface Content {
|
|
22
22
|
id: string;
|
|
@@ -38,29 +38,22 @@ export declare interface Field {
|
|
|
38
38
|
|
|
39
39
|
export declare type FieldType = 'short-text' | 'long-text' | 'number' | 'date' | 'date-time' | 'media' | 'boolean';
|
|
40
40
|
|
|
41
|
-
declare
|
|
42
|
-
| string
|
|
43
|
-
| {
|
|
44
|
-
ne?: string;
|
|
45
|
-
all?: string;
|
|
46
|
-
in?: string;
|
|
47
|
-
nin?: string;
|
|
48
|
-
lt?: string;
|
|
49
|
-
lte?: string;
|
|
50
|
-
gt?: string;
|
|
51
|
-
gte?: string;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
export declare const mapContentToModel: <Model extends BaseModel<Language>, Language = Model["language"]>(content: Content, language?: Language) => Model;
|
|
41
|
+
export declare const mapContentToModel: <M extends Model<Language> = Model<any> & Record<string, any>, Language = M["language"]>(content: Content, language?: Language) => M;
|
|
55
42
|
|
|
56
43
|
export declare interface Media {
|
|
57
44
|
name: string;
|
|
58
45
|
url: string;
|
|
59
46
|
}
|
|
60
47
|
|
|
48
|
+
declare type Model<Language = string> = {
|
|
49
|
+
id: string;
|
|
50
|
+
language?: Language;
|
|
51
|
+
};
|
|
52
|
+
|
|
61
53
|
export declare interface Options {
|
|
62
54
|
accessToken: string;
|
|
63
55
|
domain?: string;
|
|
56
|
+
secureProtocol?: boolean;
|
|
64
57
|
apiVersion?: string;
|
|
65
58
|
}
|
|
66
59
|
|
|
@@ -71,14 +64,20 @@ export declare interface Project {
|
|
|
71
64
|
contentTypes?: ApiLookup[];
|
|
72
65
|
}
|
|
73
66
|
|
|
74
|
-
declare type Query<
|
|
75
|
-
id?:
|
|
76
|
-
lastUpdate?:
|
|
77
|
-
language?:
|
|
78
|
-
contentType?:
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
67
|
+
declare type Query<M extends Model = Model> = {
|
|
68
|
+
id?: ClientFilter;
|
|
69
|
+
lastUpdate?: ClientFilter;
|
|
70
|
+
language?: ClientFilter<M['language'] extends undefined ? string : M['language']>;
|
|
71
|
+
contentType?: ClientFilter;
|
|
72
|
+
} & Partial<
|
|
73
|
+
Omit<
|
|
74
|
+
{
|
|
75
|
+
[K in keyof M as `fields.${string & K}`]: ClientFilter;
|
|
76
|
+
},
|
|
77
|
+
'fields.id' | 'fields.language'
|
|
78
|
+
>
|
|
79
|
+
>;
|
|
80
|
+
|
|
81
|
+
export declare type QueryParams<M extends Model = Model & Record<string, any>> = Pick<Query<M>, AllowedQueryKeys<M>>;
|
|
83
82
|
|
|
84
83
|
export { }
|
package/dist/index.js
CHANGED
|
@@ -1,54 +1,63 @@
|
|
|
1
|
-
const
|
|
1
|
+
const s = {
|
|
2
|
+
IS_PRODUCTION: !0,
|
|
2
3
|
DEFAULT_API_CLIENT_DOMAIN: "api.contentisland.net",
|
|
3
4
|
DEFAULT_API_CLIENT_VERSION: "1.0"
|
|
4
|
-
},
|
|
5
|
+
}, l = {
|
|
5
6
|
SESSION_KEY: "authorization",
|
|
6
7
|
SESSION_TYPE: "Bearer"
|
|
7
|
-
},
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
}, O = (t) => t.reduce((e, o) => ({ ...e, [o.name]: o.value }), {}), S = (t, e, o) => {
|
|
9
|
+
var i;
|
|
10
|
+
const n = o ?? ((i = t[0]) == null ? void 0 : i.language), r = t.filter((u) => u.language === n), c = O(r);
|
|
11
|
+
return Object.keys(c).length === 0 && console.warn(`No fields found for language "${n}" and content id "${e}"`), {
|
|
12
|
+
id: e,
|
|
13
|
+
language: n,
|
|
14
|
+
...c
|
|
15
|
+
};
|
|
16
|
+
}, N = (t, e) => Array.isArray(t == null ? void 0 : t.fields) ? S(t.fields, t.id, e) : {}, E = (t, e) => {
|
|
17
|
+
if (typeof e == "string")
|
|
18
|
+
return `${t}=${e}`;
|
|
19
|
+
if (e.in)
|
|
20
|
+
return `${t}[in]=${e.in.join(",")}`;
|
|
21
|
+
}, I = (t, e) => Object.entries(t).reduce(
|
|
22
|
+
(o, [n, r], c) => c === 0 ? E(n, r) : `${o}${e}${E(n, r)}`,
|
|
14
23
|
""
|
|
15
|
-
),
|
|
24
|
+
), g = (t) => t && Object.keys(t).length > 0 ? `?${I(t, "&")}` : "", a = {
|
|
16
25
|
PROJECT: "/project",
|
|
17
|
-
CONTENT_LIST: (
|
|
18
|
-
CONTENT: (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
const t = "https", n = e.domain ? e.domain : T.DEFAULT_API_CLIENT_DOMAIN, r = e.apiVersion ? e.apiVersion : T.DEFAULT_API_CLIENT_VERSION;
|
|
24
|
-
return `${t}://${n}/api/${r}`;
|
|
25
|
-
}, c = async (e, t) => fetch(`${_(t)}${e}`, {
|
|
26
|
+
CONTENT_LIST: (t) => `/contents${g(t)}`,
|
|
27
|
+
CONTENT: (t) => `/content${g(t)}`
|
|
28
|
+
}, _ = (t) => {
|
|
29
|
+
const o = (t.secureProtocol === void 0 ? s.IS_PRODUCTION : t.secureProtocol) ? "https" : "http", n = t.domain ? t.domain : s.DEFAULT_API_CLIENT_DOMAIN, r = t.apiVersion ? t.apiVersion : s.DEFAULT_API_CLIENT_VERSION;
|
|
30
|
+
return `${o}://${n}/api/${r}`;
|
|
31
|
+
}, T = async (t, e) => fetch(`${_(e)}${t}`, {
|
|
26
32
|
headers: {
|
|
27
|
-
[
|
|
33
|
+
[l.SESSION_KEY]: `${l.SESSION_TYPE} ${e.accessToken}`
|
|
28
34
|
}
|
|
29
|
-
}).then((
|
|
30
|
-
if (
|
|
31
|
-
return
|
|
35
|
+
}).then((o) => {
|
|
36
|
+
if (o.ok)
|
|
37
|
+
return o.json();
|
|
32
38
|
throw Error(
|
|
33
39
|
JSON.stringify({
|
|
34
|
-
status:
|
|
35
|
-
statusText:
|
|
40
|
+
status: o.status,
|
|
41
|
+
statusText: o.statusText
|
|
36
42
|
})
|
|
37
43
|
);
|
|
38
|
-
}),
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
44
|
+
}), d = (t) => {
|
|
45
|
+
const e = (n) => T(a.CONTENT_LIST(n), t), o = (n) => T(a.CONTENT(n), t);
|
|
46
|
+
return {
|
|
47
|
+
getProject: () => T(a.PROJECT, t),
|
|
48
|
+
getContentList: (n) => e(n).then(
|
|
49
|
+
(r) => Array.isArray(r) ? r.map(
|
|
50
|
+
(c) => N(c, n == null ? void 0 : n.language)
|
|
51
|
+
) : []
|
|
52
|
+
),
|
|
53
|
+
getContent: (n) => o(n).then(
|
|
54
|
+
(r) => N(r, n == null ? void 0 : n.language)
|
|
55
|
+
),
|
|
56
|
+
getRawContentList: e,
|
|
57
|
+
getRawContent: o
|
|
49
58
|
};
|
|
50
|
-
}
|
|
59
|
+
};
|
|
51
60
|
export {
|
|
52
|
-
|
|
53
|
-
|
|
61
|
+
d as createClient,
|
|
62
|
+
N as mapContentToModel
|
|
54
63
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(s,
|
|
1
|
+
(function(s,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(s=typeof globalThis<"u"?globalThis:s||self,c(s.ApiClient={}))})(this,function(s){"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"},E=e=>e.reduce((t,o)=>({...t,[o.name]:o.value}),{}),S=(e,t,o)=>{var g;const n=o??((g=e[0])==null?void 0:g.language),i=e.filter(I=>I.language===n),r=E(i);return Object.keys(r).length===0&&console.warn(`No fields found for language "${n}" and content id "${t}"`),{id:t,language:n,...r}},l=(e,t)=>Array.isArray(e==null?void 0:e.fields)?S(e.fields,e.id,t):{},d=(e,t)=>{if(typeof t=="string")return`${e}=${t}`;if(t.in)return`${e}[in]=${t.in.join(",")}`},O=(e,t)=>Object.entries(e).reduce((o,[n,i],r)=>r===0?d(n,i):`${o}${t}${d(n,i)}`,""),N=e=>e&&Object.keys(e).length>0?`?${O(e,"&")}`:"",a={PROJECT:"/project",CONTENT_LIST:e=>`/contents${N(e)}`,CONTENT:e=>`/content${N(e)}`},f=e=>{const o=(e.secureProtocol===void 0?c.IS_PRODUCTION:e.secureProtocol)?"https":"http",n=e.domain?e.domain:c.DEFAULT_API_CLIENT_DOMAIN,i=e.apiVersion?e.apiVersion:c.DEFAULT_API_CLIENT_VERSION;return`${o}://${n}/api/${i}`},T=async(e,t)=>fetch(`${f(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}))}),C=e=>{const t=n=>T(a.CONTENT_LIST(n),e),o=n=>T(a.CONTENT(n),e);return{getProject:()=>T(a.PROJECT,e),getContentList:n=>t(n).then(i=>Array.isArray(i)?i.map(r=>l(r,n==null?void 0:n.language)):[]),getContent:n=>o(n).then(i=>l(i,n==null?void 0:n.language)),getRawContentList:t,getRawContent:o}};s.createClient=C,s.mapContentToModel=l,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|