@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 CHANGED
@@ -1,9 +1,14 @@
1
- declare type AllowedQueryKeys = keyof Pick<Query, 'id' | 'contentType' | 'language'>;
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<Content[]>;
6
- getContent: (id: string, queryParam?: QueryParams) => Promise<Content>;
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 BaseModel<Language> = {
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 type Filter =
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<F = Filter> = {
75
- id?: F;
76
- lastUpdate?: F;
77
- language?: F;
78
- contentType?: F;
79
- [key: `fields.${string}`]: F;
80
- };
81
-
82
- export declare type QueryParams = Pick<Query<ClientFilter>, AllowedQueryKeys>;
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 T = {
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
- }, E = {
5
+ }, l = {
5
6
  SESSION_KEY: "authorization",
6
7
  SESSION_TYPE: "Bearer"
7
- }, N = (e, t) => {
8
- if (typeof t == "string")
9
- return `${e}=${t}`;
10
- if (t.in)
11
- return `${e}[in]=${t.in.join(",")}`;
12
- }, S = (e, t) => Object.entries(e).reduce(
13
- (n, [r, s], a) => a === 0 ? N(r, s) : `${n}${t}${N(r, s)}`,
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
- ), u = (e) => e && Object.keys(e).length > 0 ? `?${S(e, "&")}` : "", o = {
24
+ ), g = (t) => t && Object.keys(t).length > 0 ? `?${I(t, "&")}` : "", a = {
16
25
  PROJECT: "/project",
17
- CONTENT_LIST: (e) => `/contents${u(e)}`,
18
- CONTENT: (e, t) => {
19
- const n = u(t);
20
- return `/contents/${e}${n}`;
21
- }
22
- }, _ = (e) => {
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
- [E.SESSION_KEY]: `${E.SESSION_TYPE} ${t.accessToken}`
33
+ [l.SESSION_KEY]: `${l.SESSION_TYPE} ${e.accessToken}`
28
34
  }
29
- }).then((n) => {
30
- if (n.ok)
31
- return n.json();
35
+ }).then((o) => {
36
+ if (o.ok)
37
+ return o.json();
32
38
  throw Error(
33
39
  JSON.stringify({
34
- status: n.status,
35
- statusText: n.statusText
40
+ status: o.status,
41
+ statusText: o.statusText
36
42
  })
37
43
  );
38
- }), $ = (e) => ({
39
- getProject: () => c(o.PROJECT, e),
40
- getContentList: (t) => c(o.CONTENT_LIST(t), e),
41
- getContent: (t, n) => c(o.CONTENT(t, n), e)
42
- }), g = (e) => e.reduce((t, n) => ({ ...t, [n.name]: n.value }), {}), O = (e, t, n) => {
43
- var i;
44
- const r = n ?? ((i = e[0]) == null ? void 0 : i.language), s = e.filter((l) => l.language === r), a = g(s);
45
- return Object.keys(a).length === 0 && console.warn(`No fields found for language "${r}" and content id "${t}"`), {
46
- id: t,
47
- language: r,
48
- ...a
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
- }, d = (e, t) => Array.isArray(e == null ? void 0 : e.fields) ? O(e.fields, e.id, t) : {};
59
+ };
51
60
  export {
52
- $ as createClient,
53
- d as mapContentToModel
61
+ d as createClient,
62
+ N as mapContentToModel
54
63
  };
@@ -1 +1 @@
1
- (function(s,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(s=typeof globalThis<"u"?globalThis:s||self,r(s.ApiClient={}))})(this,function(s){"use strict";const r={DEFAULT_API_CLIENT_DOMAIN:"api.contentisland.net",DEFAULT_API_CLIENT_VERSION:"1.0"},u={SESSION_KEY:"authorization",SESSION_TYPE:"Bearer"},l=(e,t)=>{if(typeof t=="string")return`${e}=${t}`;if(t.in)return`${e}[in]=${t.in.join(",")}`},N=(e,t)=>Object.entries(e).reduce((n,[o,a],i)=>i===0?l(o,a):`${n}${t}${l(o,a)}`,""),d=e=>e&&Object.keys(e).length>0?`?${N(e,"&")}`:"",c={PROJECT:"/project",CONTENT_LIST:e=>`/contents${d(e)}`,CONTENT:(e,t)=>{const n=d(t);return`/contents/${e}${n}`}},S=e=>{const t="https",n=e.domain?e.domain:r.DEFAULT_API_CLIENT_DOMAIN,o=e.apiVersion?e.apiVersion:r.DEFAULT_API_CLIENT_VERSION;return`${t}://${n}/api/${o}`},T=async(e,t)=>fetch(`${S(t)}${e}`,{headers:{[u.SESSION_KEY]:`${u.SESSION_TYPE} ${t.accessToken}`}}).then(n=>{if(n.ok)return n.json();throw Error(JSON.stringify({status:n.status,statusText:n.statusText}))}),f=e=>({getProject:()=>T(c.PROJECT,e),getContentList:t=>T(c.CONTENT_LIST(t),e),getContent:(t,n)=>T(c.CONTENT(t,n),e)}),g=e=>e.reduce((t,n)=>({...t,[n.name]:n.value}),{}),O=(e,t,n)=>{var E;const o=n??((E=e[0])==null?void 0:E.language),a=e.filter($=>$.language===o),i=g(a);return Object.keys(i).length===0&&console.warn(`No fields found for language "${o}" and content id "${t}"`),{id:t,language:o,...i}},_=(e,t)=>Array.isArray(e==null?void 0:e.fields)?O(e.fields,e.id,t):{};s.createClient=f,s.mapContentToModel=_,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
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"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@content-island/api-client",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "Content Island - REST API Client",
5
5
  "private": false,
6
6
  "sideEffects": false,