@content-island/api-client 0.2.1 → 0.3.1

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 ADDED
@@ -0,0 +1,105 @@
1
+ # @content-island/api-client
2
+
3
+ ## Installation
4
+
5
+ ```bash
6
+ npm install @content-island/api-client
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ ```typescript
12
+ import { createClient } from '@content-island/api-client';
13
+
14
+ const client = createClient({ accessToken: <your-token>});
15
+
16
+ const project = client.getProject(); // Retrieve the project by access token
17
+
18
+ const contents = client.getContentList(); // Retrieve the list of contents in the project
19
+
20
+ const postContents = client.getContentList({ contentType: 'post'}); // Retrieve the list of contents in the project filtered by content type, for example 'post'
21
+
22
+ const content = client.getContent('content-id'); // Retrieve a content by id
23
+
24
+ const content = client.getContent('content-id', { contentType: 'post'}); // Retrieve a content by id filtered by content type, for example 'post'
25
+
26
+ ```
27
+
28
+ ## API
29
+
30
+ ### `createClient(options): endpoints`
31
+
32
+ Creates a client instance.
33
+
34
+ #### `options`
35
+
36
+ | name | type | required | description |
37
+ | ------------- | -------- | -------- | ----------------------------------------------------------- |
38
+ | `accessToken` | `string` | `true` | The access token of the project |
39
+ | `domain` | `string` | `false` | The domain of the project. Default: `api.contentisland.net` |
40
+ | `apiVersion` | `string` | `false` | The version of the API. Default: `1.0` |
41
+
42
+ #### `endpoints`
43
+
44
+ ##### `getProject()`
45
+
46
+ Retrieves the project by access token.
47
+
48
+ ##### `getContentList(queryParams)`
49
+
50
+ Retrieves the list of contents in the project.
51
+
52
+ ##### `getContent(contentId, queryParams)`
53
+
54
+ Retrieves a content by id.
55
+
56
+ #### `queryParams`
57
+
58
+ The query parameters to filter the list of contents.
59
+
60
+ | param | Description |
61
+ | ------------- | -------------------------------------------------------------------- |
62
+ | `contentType` | The content type to filter the list of contents. For example: `post` |
63
+
64
+ ### `mapFieldListToModel(fieldList)`
65
+
66
+ Maps a list of fields to a model.
67
+
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
+ ];
95
+
96
+ interface Post {
97
+ title: string;
98
+ body: string;
99
+ order: number;
100
+ }
101
+
102
+ const post = mapFieldListToModel<Post>(fields);
103
+
104
+ console.log(post); // { title: 'My title', body: '# My body', order: 1 }
105
+ ```
@@ -1,33 +1,42 @@
1
- const r = {
2
- sessionKey: "authorization",
3
- sessionType: "Bearer"
4
- }, i = {
5
- DEFAULT_B2B_BASE_URL: "https://dev-content-island-b2b.azurewebsites.net/api/1.0"
6
- }, l = (e) => {
7
- const { accessToken: t, baseUrl: n = i.DEFAULT_B2B_BASE_URL } = e;
8
- return {
9
- get: async (s) => fetch(`${n}/${s}`, {
10
- headers: {
11
- [r.sessionKey]: `${r.sessionType} ${t}`
12
- }
13
- }).then((o) => o.json())
14
- };
15
- }, u = (e, t) => {
16
- var n;
17
- return (n = e.find((s) => s.name === t)) == null ? void 0 : n.value;
18
- }, a = (e) => Array.isArray(e.filterList) ? `?${y(e.filterList, "&")}` : "", y = (e, t) => Array.isArray(e) ? e.reduce(
19
- (n, s, o, c) => c.length - 1 === o ? n += s : n += `${s}${t} `,
1
+ const a = {
2
+ IS_PRODUCTION: !0,
3
+ DEFAULT_API_CLIENT_DOMAIN: "api.contentisland.net",
4
+ DEFAULT_API_CLIENT_VERSION: "1.0"
5
+ }, s = {
6
+ SESSION_KEY: "authorization",
7
+ SESSION_TYPE: "Bearer"
8
+ }, S = (t, e) => Object.entries(t).reduce(
9
+ (n, [o, T], E) => E === 0 ? `${o}=${T}` : `${n}${e}${o}=${T}`,
20
10
  ""
21
- ) : "", $ = {
22
- project: "/project",
23
- contentList: (e) => `/contents${a(e)}`,
24
- content: (e) => {
25
- const t = a(e);
26
- return `/contents/${e.id}${t}`;
11
+ ), N = (t) => t && Object.keys(t).length > 0 ? `?${S(t, "&")}` : "", r = {
12
+ PROYECT: "/project",
13
+ CONTENT_LIST: (t) => `/contents${N(t)}`,
14
+ CONTENT: (t, e) => {
15
+ const n = N(e);
16
+ return `/contents/${t}${n}`;
27
17
  }
28
- };
18
+ }, _ = (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}`, {
22
+ headers: {
23
+ [s.SESSION_KEY]: `${s.SESSION_TYPE} ${e.accessToken}`
24
+ }
25
+ }).then((n) => {
26
+ if (n.ok)
27
+ return n.json();
28
+ throw Error(
29
+ JSON.stringify({
30
+ status: n.status,
31
+ statusText: n.statusText
32
+ })
33
+ );
34
+ }), 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) : {};
29
39
  export {
30
- $ as API_URLS,
31
- l as createContentIslandClient,
32
- u as mapFieldToValue
40
+ O as createClient,
41
+ i as mapFieldListToModel
33
42
  };
@@ -1 +1 @@
1
- (function(t,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(t=typeof globalThis<"u"?globalThis:t||self,o(t.ApiClient={}))})(this,function(t){"use strict";const o={sessionKey:"authorization",sessionType:"Bearer"},c={DEFAULT_B2B_BASE_URL:"https://dev-content-island-b2b.azurewebsites.net/api/1.0"},d=e=>{const{accessToken:n,baseUrl:s=c.DEFAULT_B2B_BASE_URL}=e;return{get:async i=>fetch(`${s}/${i}`,{headers:{[o.sessionKey]:`${o.sessionType} ${n}`}}).then(r=>r.json())}},l=(e,n)=>{var s;return(s=e.find(i=>i.name===n))==null?void 0:s.value},a=e=>Array.isArray(e.filterList)?`?${u(e.filterList,"&")}`:"",u=(e,n)=>Array.isArray(e)?e.reduce((s,i,r,f)=>f.length-1===r?s+=i:s+=`${i}${n} `,""):"",y={project:"/project",contentList:e=>`/contents${a(e)}`,content:e=>{const n=a(e);return`/contents/${e.id}${n}`}};t.API_URLS=y,t.createContentIslandClient=d,t.mapFieldToValue=l,Object.defineProperty(t,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"},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"})});
package/dist/client.d.ts CHANGED
@@ -1,9 +1,13 @@
1
- interface Options {
1
+ import { Project, Content } from '@content-island/b2b-api-model';
2
+ import { QueryParams } from './model.js';
3
+ export interface Options {
2
4
  accessToken: string;
3
- baseUrl?: string;
5
+ domain?: string;
6
+ apiVersion?: string;
4
7
  }
5
- export interface ContentIslandClient {
6
- get: <Entity>(url: string) => Promise<Entity>;
8
+ export interface ApiClient {
9
+ getProject: () => Promise<Project>;
10
+ getContentList: (queryParam?: QueryParams) => Promise<Content[]>;
11
+ getContent: (id: string, queryParam?: QueryParams) => Promise<Content>;
7
12
  }
8
- export declare const createContentIslandClient: (options: Options) => ContentIslandClient;
9
- export {};
13
+ export declare const createClient: (options: Options) => ApiClient;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,7 +1,12 @@
1
- export declare const authConstants: {
2
- sessionKey: string;
3
- sessionType: string;
1
+ export declare const ENV_CONSTANTS: {
2
+ IS_PRODUCTION: boolean;
3
+ DEFAULT_API_CLIENT_DOMAIN: string;
4
+ DEFAULT_API_CLIENT_VERSION: string;
4
5
  };
5
- export declare const envConstants: {
6
- DEFAULT_B2B_BASE_URL: string;
6
+ export declare const AUTH_CONSTANTS: {
7
+ SESSION_KEY: string;
8
+ SESSION_TYPE: string;
9
+ };
10
+ export declare const ALLOWED_QUERIES: {
11
+ contentType: string;
7
12
  };
@@ -0,0 +1,2 @@
1
+ import { QueryParams } from './model.js';
2
+ export declare const formatQuery: (queryParams: QueryParams) => string;
@@ -0,0 +1 @@
1
+ export {};
package/dist/index.d.ts CHANGED
@@ -1,4 +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';
4
- export * from './urls';
2
+ export { createClient } from './client.js';
3
+ export type { ApiClient, Options } from './client.js';
4
+ export { mapFieldListToModel } from './mappers.js';
package/dist/mappers.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  import { Field } from '@content-island/b2b-api-model';
2
- export declare const mapFieldToValue: <Entity>(fields: Field[], key: keyof Entity) => any;
2
+ export declare const mapFieldListToModel: <Model>(fields: Field[]) => Model;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import { ALLOWED_QUERIES } from './constants.js';
2
+ export type QueryParams = Partial<typeof ALLOWED_QUERIES>;
package/dist/urls.d.ts CHANGED
@@ -1,12 +1,6 @@
1
- interface BaseParams {
2
- filterList: string[];
3
- }
4
- interface ContentParams extends BaseParams {
5
- id: string;
6
- }
1
+ import { QueryParams } from './model.js';
7
2
  export declare const API_URLS: {
8
- project: string;
9
- contentList: (params: BaseParams) => string;
10
- content: (params: ContentParams) => string;
3
+ PROYECT: string;
4
+ CONTENT_LIST: (queryParam: QueryParams) => string;
5
+ CONTENT: (id: string, querParams: QueryParams) => string;
11
6
  };
12
- export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@content-island/api-client",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
4
  "description": "Content Island - REST API Client",
5
5
  "private": false,
6
6
  "author": "Lemoncode",
@@ -23,16 +23,19 @@
23
23
  "prebuild": "if-env NODE_ENV=development && sh ./create-dev-env.sh || echo noop",
24
24
  "build": "vite build",
25
25
  "type-check": "tsc --noEmit",
26
- "type-check:watch": "npm run type-check -- --watch --preserveWatchOutput"
26
+ "type-check:watch": "npm run type-check -- --watch --preserveWatchOutput",
27
+ "test": "vitest run -c ./config/test/config.ts",
28
+ "test:watch": "vitest -c ./config/test/config.ts"
27
29
  },
28
30
  "dependencies": {
29
- "@content-island/b2b-api-model": "^0.2.0"
31
+ "@content-island/b2b-api-model": "^0.2.1"
30
32
  },
31
33
  "devDependencies": {
32
34
  "dotenv": "^16.3.1",
33
35
  "if-env": "^1.0.4",
34
36
  "typescript": "^5.2.2",
35
37
  "vite": "^4.4.9",
36
- "vite-plugin-dts": "^3.5.3"
38
+ "vite-plugin-dts": "^3.5.3",
39
+ "vitest": "^0.34.6"
37
40
  }
38
41
  }