@content-island/api-client 0.16.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 CHANGED
@@ -1,5 +1,5 @@
1
- declare type AllowedModelFields<M extends Model, ValueType> = Partial<Omit<{
2
- [K in keyof M as IsPrimitive<NonNullable<M[K]>> extends true ? `fields.${string & K}` : never]?: ValueType;
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 {
@@ -18,7 +18,7 @@ declare interface BaseModel {
18
18
  type: ModelType;
19
19
  }
20
20
 
21
- export declare type ClientFilter<Type = string | boolean> = Type | {
21
+ export declare type ClientFilter<Type = string | boolean | number> = Type | {
22
22
  in?: Type[];
23
23
  ne?: Type;
24
24
  };
@@ -97,12 +97,12 @@ export declare type FieldType =
97
97
  | RelatedModelType;
98
98
 
99
99
  declare type FilterableFields<M extends Model = Model> = {
100
- id?: ClientFilter;
101
- lastUpdate?: ClientFilter;
100
+ id?: ClientFilter<string>;
101
+ lastUpdate?: ClientFilter<string>;
102
102
  language?: M['language'];
103
- contentType?: ClientFilter;
103
+ contentType?: ClientFilter<string>;
104
104
  includeRelatedContent?: boolean;
105
- } & AllowedModelFields<M, ClientFilter>;
105
+ } & AllowedFields<M, 'filter'>;
106
106
 
107
107
  declare type IsPrimitive<T> = [T] extends [string | number | boolean] ? true : false;
108
108
 
@@ -342,7 +342,7 @@ declare type RelatedModelType = `${string}|${string}`;
342
342
  declare type SortableFields<M extends Model> = {
343
343
  contentType?: SortOrder;
344
344
  lastUpdate?: SortOrder;
345
- } & AllowedModelFields<M, SortOrder>;
345
+ } & AllowedFields<M, 'sort'>;
346
346
 
347
347
  declare type SortOrder = 'asc' | 'desc';
348
348
 
package/dist/index.js CHANGED
@@ -1,26 +1,27 @@
1
- const N = (t, e) => {
2
- if (typeof e == "string" || typeof e == "boolean")
3
- return `${t}=${e}`;
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
- }, A = (t) => Object.entries(t).map(([e, n]) => `${e}:${n}`).join(","), O = (t, e) => {
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
- }, _ = (t, e) => Object.entries(t).reduce((n, [o, s], a) => {
12
- if (o === "sort" && typeof s == "object" && s !== null && !Array.isArray(s)) {
13
- const r = A(s);
14
- return a === 0 ? `sort=${r}` : `${n}${e}sort=${r}`;
15
- } else if (o === "pagination" && typeof s == "object" && s !== null) {
16
- const r = O(s, e);
17
- return a === 0 ? r : `${n}${e}${r}`;
18
- } else if (s !== void 0 && o !== "sort" && o !== "pagination") {
19
- const r = s;
20
- return a === 0 ? N(o, r) : `${n}${e}${N(o, r)}`;
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
- }, ""), S = (t) => t && Object.keys(t).length > 0 ? `?${_(t, "&")}` : "", $ = {
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
- }, l = (t) => {
32
- const n = (t.secureProtocol === void 0 ? $.IS_PRODUCTION : t.secureProtocol) ? "https" : "http", o = t.domain ? t.domain : $.DEFAULT_API_CLIENT_DOMAIN, s = t.apiVersion ? t.apiVersion : $.DEFAULT_API_CLIENT_VERSION;
33
- return `${n}://${o}/api/${s}`;
34
- }, d = async (t, e) => fetch(`${l(e)}${t}`, {
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
- }), C = async (t, e, n) => fetch(`${l(n)}${t}`, {
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: d,
67
- put: C
68
- }, I = (t) => t == null ? void 0 : t.includes("|"), p = (t, e) => Array.isArray(t) ? t.map(e) : [], L = (t, e) => t.isArray ? p(t.value, (n) => E(n, e)) : E(t.value, e), f = (t) => I(t.type) && (t.isArray ? t.value.every((e) => typeof e == "object" && "contentType" in e) : typeof t.value == "object" && "contentType" in t.value), h = (t, e) => t.reduce(
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]: f(o) ? L(o, e) : o.value
72
+ [o.name]: F(o) ? y(o, e) : o.value
72
73
  }),
73
74
  {}
74
- ), y = (t, e, n, o) => {
75
+ ), R = (t, e, n, o) => {
75
76
  var c;
76
- const s = n ?? ((c = t[0]) == null ? void 0 : c.language), a = t.filter((g) => g.language === s), r = h(a, s);
77
- return Object.keys(r).length === 0 && console.warn(`No fields found for language "${s}" and content id "${e}"`), {
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: s,
80
+ language: r,
80
81
  lastUpdate: o,
81
- ...r
82
+ ...s
82
83
  };
83
- }, E = (t, e) => Array.isArray(t == null ? void 0 : t.fields) ? y(t.fields, t.id, e, t.lastUpdate) : {}, u = {
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${S(t)}`,
86
- CONTENT: (t) => `/content${S(t)}`,
87
- CONTENT_LIST_SIZE: (t) => t ? `/contents/size${S(t)}` : "/contents/size"
88
- }, j = (t) => {
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), s = async () => {
90
- var r;
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 (r = a == null ? void 0 : a.languages) == null ? void 0 : r[0];
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 r = await e(a), c = (a == null ? void 0 : a.language) ?? await s();
98
- return Array.isArray(r) ? r.map((g) => E(g, c)) : [];
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 r = await n(a), c = (a == null ? void 0 : a.language) ?? await s();
102
- return E(r, c);
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, r, c) => T.put(
108
+ updateContentFieldValue: (a, s, c) => T.put(
108
109
  `${u.CONTENT(null)}/${a}`,
109
110
  {
110
- id: r,
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
- j as createClient,
119
+ D as createClient,
119
120
  E as mapContentToModel
120
121
  };
@@ -1 +1 @@
1
- (function(i,T){typeof exports=="object"&&typeof module<"u"?T(exports):typeof define=="function"&&define.amd?define(["exports"],T):(i=typeof globalThis<"u"?globalThis:i||self,T(i.ApiClient={}))})(this,function(i){"use strict";const T=(t,e)=>{if(typeof e=="string"||typeof e=="boolean")return`${t}=${e}`;if(e.in)return`${t}[in]=${e.in.join(",")}`;if(e.ne)return`${t}[ne]=${e.ne}`},A=t=>Object.entries(t).map(([e,n])=>`${e}:${n}`).join(","),O=(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},C=(t,e)=>Object.entries(t).reduce((n,[a,r],o)=>{if(a==="sort"&&typeof r=="object"&&r!==null&&!Array.isArray(r)){const s=A(r);return o===0?`sort=${s}`:`${n}${e}sort=${s}`}else if(a==="pagination"&&typeof r=="object"&&r!==null){const s=O(r,e);return o===0?s:`${n}${e}${s}`}else if(r!==void 0&&a!=="sort"&&a!=="pagination"){const s=r;return o===0?T(a,s):`${n}${e}${T(a,s)}`}return n},""),S=t=>t&&Object.keys(t).length>0?`?${C(t,"&")}`:"",d={IS_PRODUCTION:!0,DEFAULT_API_CLIENT_DOMAIN:"api.contentisland.net",DEFAULT_API_CLIENT_VERSION:"1.0"},u={SESSION_KEY:"authorization",SESSION_TYPE:"Bearer",METADATA_KEY:"x-metadata"},N=t=>{const n=(t.secureProtocol===void 0?d.IS_PRODUCTION:t.secureProtocol)?"https":"http",a=t.domain?t.domain:d.DEFAULT_API_CLIENT_DOMAIN,r=t.apiVersion?t.apiVersion:d.DEFAULT_API_CLIENT_VERSION;return`${n}://${a}/api/${r}`},g={get:async(t,e)=>fetch(`${N(e)}${t}`,{headers:{[u.SESSION_KEY]:`${u.SESSION_TYPE} ${e.accessToken}`,...e.metadata?{[u.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(`${N(n)}${t}`,{method:"PUT",headers:{"Content-Type":"application/json",[u.SESSION_KEY]:`${u.SESSION_TYPE} ${n.accessToken}`,...n.metadata?{[u.METADATA_KEY]:n.metadata}:{}},body:JSON.stringify(e)}).then(a=>{if(a.ok)return!0;throw Error(JSON.stringify({status:a.status,statusText:a.statusText}))})},_=t=>t==null?void 0:t.includes("|"),p=(t,e)=>Array.isArray(t)?t.map(e):[],f=(t,e)=>t.isArray?p(t.value,n=>l(n,e)):l(t.value,e),I=t=>_(t.type)&&(t.isArray?t.value.every(e=>typeof e=="object"&&"contentType"in e):typeof t.value=="object"&&"contentType"in t.value),L=(t,e)=>t.reduce((n,a)=>({...n,[a.name]:I(a)?f(a,e):a.value}),{}),h=(t,e,n,a)=>{var c;const r=n??((c=t[0])==null?void 0:c.language),o=t.filter($=>$.language===r),s=L(o,r);return Object.keys(s).length===0&&console.warn(`No fields found for language "${r}" and content id "${e}"`),{id:e,language:r,lastUpdate:a,...s}},l=(t,e)=>Array.isArray(t==null?void 0:t.fields)?h(t.fields,t.id,e,t.lastUpdate):{},E={PROJECT:"/project",CONTENT_LIST:t=>`/contents${S(t)}`,CONTENT:t=>`/content${S(t)}`,CONTENT_LIST_SIZE:t=>t?`/contents/size${S(t)}`:"/contents/size"},y=t=>{const e=o=>g.get(E.CONTENT_LIST(o),t),n=o=>g.get(E.CONTENT(o),t),a=()=>g.get(E.PROJECT,t),r=async()=>{var s;const o=await a();return(s=o==null?void 0:o.languages)==null?void 0:s[0]};return{getProject:a,getContentList:async o=>{const s=await e(o),c=(o==null?void 0:o.language)??await r();return Array.isArray(s)?s.map($=>l($,c)):[]},getContent:async o=>{const s=await n(o),c=(o==null?void 0:o.language)??await r();return l(s,c)},getRawContentList:e,getRawContent:n,getContentListSize:o=>g.get(E.CONTENT_LIST_SIZE(o),t),updateContentFieldValue:(o,s,c)=>g.put(`${E.CONTENT(null)}/${o}`,{id:s,value:c},t)}};i.createClient=y,i.mapContentToModel=l,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
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"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@content-island/api-client",
3
- "version": "0.16.0",
3
+ "version": "0.17.0",
4
4
  "description": "Content Island - REST API Client",
5
5
  "private": false,
6
6
  "sideEffects": false,