@content-island/api-client 0.3.0 → 0.4.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/README.md CHANGED
@@ -61,45 +61,51 @@ The query parameters to filter the list of contents.
61
61
  | ------------- | -------------------------------------------------------------------- |
62
62
  | `contentType` | The content type to filter the list of contents. For example: `post` |
63
63
 
64
- ### `mapFieldListToModel(fieldList)`
64
+ ### `mapContentToModel(content)`
65
65
 
66
66
  Maps a list of fields to a model.
67
67
 
68
68
  ```typescript
69
- const fields: Field[] = [
70
- {
71
- id: '1',
72
- name: 'title',
73
- value: 'My title',
74
- type: 'short-text',
75
- isArray: false,
76
- language: 'en',
77
- },
78
- {
79
- id: '2',
80
- name: 'body',
81
- value: '# My body',
82
- type: 'long-text',
83
- isArray: false,
84
- language: 'en',
85
- },
86
- {
87
- id: '3',
88
- name: 'order',
89
- value: 1,
90
- type: 'number',
91
- isArray: false,
92
- language: 'en',
93
- },
94
- ];
69
+ const content: Content = {
70
+ id: '1',
71
+ contentType: { id: '10', name: 'post' },
72
+ lastUpdate: '2023-10-20T00:00:00.000Z',
73
+ fields: [
74
+ {
75
+ id: '100',
76
+ name: 'title',
77
+ value: 'My title',
78
+ type: 'short-text',
79
+ isArray: false,
80
+ language: 'en',
81
+ },
82
+ {
83
+ id: '200',
84
+ name: 'body',
85
+ value: '# My body',
86
+ type: 'long-text',
87
+ isArray: false,
88
+ language: 'en',
89
+ },
90
+ {
91
+ id: '300',
92
+ name: 'order',
93
+ value: 1,
94
+ type: 'number',
95
+ isArray: false,
96
+ language: 'en',
97
+ },
98
+ ],
99
+ };
95
100
 
96
101
  interface Post {
102
+ id: string;
97
103
  title: string;
98
104
  body: string;
99
105
  order: number;
100
106
  }
101
107
 
102
- const post = mapFieldListToModel<Post>(fields);
108
+ const post = mapContentToModel<Post>(fields);
103
109
 
104
- console.log(post); // { title: 'My title', body: '# My body', order: 1 }
110
+ console.log(post); // { id: '1', title: 'My title', body: '# My body', order: 1 }
105
111
  ```
@@ -2,41 +2,49 @@ const a = {
2
2
  IS_PRODUCTION: !0,
3
3
  DEFAULT_API_CLIENT_DOMAIN: "api.contentisland.net",
4
4
  DEFAULT_API_CLIENT_VERSION: "1.0"
5
- }, s = {
5
+ }, c = {
6
6
  SESSION_KEY: "authorization",
7
7
  SESSION_TYPE: "Bearer"
8
+ }, i = (t, e) => {
9
+ if (typeof e == "string")
10
+ return `${t}=${e}`;
11
+ if (e.in)
12
+ return `${t}[in]=${e.in.join(",")}`;
8
13
  }, S = (t, e) => Object.entries(t).reduce(
9
- (n, [o, T], E) => E === 0 ? `${o}=${T}` : `${n}${e}${o}=${T}`,
14
+ (r, [n, T], E) => E === 0 ? i(n, T) : `${r}${e}${i(n, T)}`,
10
15
  ""
11
- ), N = (t) => t && Object.keys(t).length > 0 ? `?${S(t, "&")}` : "", r = {
16
+ ), N = (t) => t && Object.keys(t).length > 0 ? `?${S(t, "&")}` : "", s = {
12
17
  PROYECT: "/project",
13
18
  CONTENT_LIST: (t) => `/contents${N(t)}`,
14
19
  CONTENT: (t, e) => {
15
- const n = N(e);
16
- return `/contents/${t}${n}`;
20
+ const r = N(e);
21
+ return `/contents/${t}${r}`;
17
22
  }
18
23
  }, _ = (t) => {
19
- const e = "https", n = t.domain ? t.domain : a.DEFAULT_API_CLIENT_DOMAIN, o = t.apiVersion ? t.apiVersion : a.DEFAULT_API_CLIENT_VERSION;
20
- return `${e}://${n}/api/${o}`;
21
- }, c = async (t, e) => fetch(`${_(e)}${t}`, {
24
+ const e = "https", r = t.domain ? t.domain : a.DEFAULT_API_CLIENT_DOMAIN, n = t.apiVersion ? t.apiVersion : a.DEFAULT_API_CLIENT_VERSION;
25
+ return `${e}://${r}/api/${n}`;
26
+ }, o = async (t, e) => fetch(`${_(e)}${t}`, {
22
27
  headers: {
23
- [s.SESSION_KEY]: `${s.SESSION_TYPE} ${e.accessToken}`
28
+ [c.SESSION_KEY]: `${c.SESSION_TYPE} ${e.accessToken}`
24
29
  }
25
- }).then((n) => {
26
- if (n.ok)
27
- return n.json();
30
+ }).then((r) => {
31
+ if (r.ok)
32
+ return r.json();
28
33
  throw Error(
29
34
  JSON.stringify({
30
- status: n.status,
31
- statusText: n.statusText
35
+ status: r.status,
36
+ statusText: r.statusText
32
37
  })
33
38
  );
34
39
  }), O = (t) => ({
35
- getProject: () => c(r.PROYECT, t),
36
- getContentList: (e) => c(r.CONTENT_LIST(e), t),
37
- getContent: (e, n) => c(r.CONTENT(e, n), t)
38
- }), I = (t) => t.reduce((e, n) => ({ ...e, [n.name]: n.value }), {}), i = (t) => Array.isArray(t) ? I(t) : {};
40
+ getProject: () => o(s.PROYECT, t),
41
+ getContentList: (e) => o(s.CONTENT_LIST(e), t),
42
+ getContent: (e, r) => o(s.CONTENT(e, r), t)
43
+ }), I = (t) => t.reduce((e, r) => ({ ...e, [r.name]: r.value }), {}), u = (t) => Array.isArray(t == null ? void 0 : t.fields) ? {
44
+ id: t.id,
45
+ ...I(t.fields)
46
+ } : {};
39
47
  export {
40
48
  O as createClient,
41
- i as mapFieldListToModel
49
+ u as mapContentToModel
42
50
  };
@@ -1 +1 @@
1
- (function(o,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(o=typeof globalThis<"u"?globalThis:o||self,r(o.ApiClient={}))})(this,function(o){"use strict";const r={IS_PRODUCTION:!0,DEFAULT_API_CLIENT_DOMAIN:"api.contentisland.net",DEFAULT_API_CLIENT_VERSION:"1.0"},T={SESSION_KEY:"authorization",SESSION_TYPE:"Bearer"},S=(t,e)=>Object.entries(t).reduce((n,[i,N],_)=>_===0?`${i}=${N}`:`${n}${e}${i}=${N}`,""),a=t=>t&&Object.keys(t).length>0?`?${S(t,"&")}`:"",c={PROYECT:"/project",CONTENT_LIST:t=>`/contents${a(t)}`,CONTENT:(t,e)=>{const n=a(e);return`/contents/${t}${n}`}},u=t=>{const e="https",n=t.domain?t.domain:r.DEFAULT_API_CLIENT_DOMAIN,i=t.apiVersion?t.apiVersion:r.DEFAULT_API_CLIENT_VERSION;return`${e}://${n}/api/${i}`},s=async(t,e)=>fetch(`${u(e)}${t}`,{headers:{[T.SESSION_KEY]:`${T.SESSION_TYPE} ${e.accessToken}`}}).then(n=>{if(n.ok)return n.json();throw Error(JSON.stringify({status:n.status,statusText:n.statusText}))}),E=t=>({getProject:()=>s(c.PROYECT,t),getContentList:e=>s(c.CONTENT_LIST(e),t),getContent:(e,n)=>s(c.CONTENT(e,n),t)}),d=t=>t.reduce((e,n)=>({...e,[n.name]:n.value}),{}),O=t=>Array.isArray(t)?d(t):{};o.createClient=E,o.mapFieldListToModel=O,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
1
+ (function(o,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(o=typeof globalThis<"u"?globalThis:o||self,r(o.ApiClient={}))})(this,function(o){"use strict";const r={IS_PRODUCTION:!0,DEFAULT_API_CLIENT_DOMAIN:"api.contentisland.net",DEFAULT_API_CLIENT_VERSION:"1.0"},a={SESSION_KEY:"authorization",SESSION_TYPE:"Bearer"},c=(e,t)=>{if(typeof t=="string")return`${e}=${t}`;if(t.in)return`${e}[in]=${t.in.join(",")}`},d=(e,t)=>Object.entries(e).reduce((n,[i,N],I)=>I===0?c(i,N):`${n}${t}${c(i,N)}`,""),u=e=>e&&Object.keys(e).length>0?`?${d(e,"&")}`:"",s={PROYECT:"/project",CONTENT_LIST:e=>`/contents${u(e)}`,CONTENT:(e,t)=>{const n=u(t);return`/contents/${e}${n}`}},E=e=>{const t="https",n=e.domain?e.domain:r.DEFAULT_API_CLIENT_DOMAIN,i=e.apiVersion?e.apiVersion:r.DEFAULT_API_CLIENT_VERSION;return`${t}://${n}/api/${i}`},T=async(e,t)=>fetch(`${E(t)}${e}`,{headers:{[a.SESSION_KEY]:`${a.SESSION_TYPE} ${t.accessToken}`}}).then(n=>{if(n.ok)return n.json();throw Error(JSON.stringify({status:n.status,statusText:n.statusText}))}),S=e=>({getProject:()=>T(s.PROYECT,e),getContentList:t=>T(s.CONTENT_LIST(t),e),getContent:(t,n)=>T(s.CONTENT(t,n),e)}),O=e=>e.reduce((t,n)=>({...t,[n.name]:n.value}),{}),_=e=>Array.isArray(e==null?void 0:e.fields)?{id:e.id,...O(e.fields)}:{};o.createClient=S,o.mapContentToModel=_,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
package/dist/client.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Project, Content } from '@content-island/b2b-api-model';
2
- import { QueryParams } from './model';
2
+ import { QueryParams } from './model.js';
3
3
  export interface Options {
4
4
  accessToken: string;
5
5
  domain?: string;
@@ -9,4 +9,5 @@ export declare const AUTH_CONSTANTS: {
9
9
  };
10
10
  export declare const ALLOWED_QUERIES: {
11
11
  contentType: string;
12
+ id: string;
12
13
  };
package/dist/helpers.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { QueryParams } from './model';
1
+ import { QueryParams } from './model.js';
2
2
  export declare const formatQuery: (queryParams: QueryParams) => string;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export type { Project, Content, Field, FieldType, Resource, ApiLookup } from '@content-island/b2b-api-model';
2
- export * from './client';
3
- export * from './mappers';
2
+ export { createClient } from './client.js';
3
+ export type { ApiClient, Options } from './client.js';
4
+ export { mapContentToModel } from './mappers.js';
package/dist/mappers.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { Field } from '@content-island/b2b-api-model';
2
- export declare const mapFieldListToModel: <Model>(fields: Field[]) => Model;
1
+ import { Content } from '@content-island/b2b-api-model';
2
+ export declare const mapContentToModel: <Model>(content: Content) => Model;
package/dist/model.d.ts CHANGED
@@ -1,2 +1,7 @@
1
- import { ALLOWED_QUERIES } from './constants';
2
- export type QueryParams = Partial<typeof ALLOWED_QUERIES>;
1
+ import { Query } from '@content-island/b2b-api-model';
2
+ import { ALLOWED_QUERIES } from './constants.js';
3
+ export type Filter = string | {
4
+ in?: string[];
5
+ };
6
+ export type AllowedQuery = keyof typeof ALLOWED_QUERIES;
7
+ export type QueryParams = Pick<Query<Filter>, AllowedQuery>;
package/dist/urls.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { QueryParams } from './model';
1
+ import { QueryParams } from './model.js';
2
2
  export declare const API_URLS: {
3
3
  PROYECT: string;
4
4
  CONTENT_LIST: (queryParam: QueryParams) => string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@content-island/api-client",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Content Island - REST API Client",
5
5
  "private": false,
6
6
  "author": "Lemoncode",
@@ -28,7 +28,7 @@
28
28
  "test:watch": "vitest -c ./config/test/config.ts"
29
29
  },
30
30
  "dependencies": {
31
- "@content-island/b2b-api-model": "^0.2.0"
31
+ "@content-island/b2b-api-model": "^0.3.0"
32
32
  },
33
33
  "devDependencies": {
34
34
  "dotenv": "^16.3.1",