@content-island/api-client 0.2.1 → 0.3.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 +105 -0
- package/dist/api-client.js +38 -29
- package/dist/api-client.umd.cjs +1 -1
- package/dist/client.d.ts +10 -6
- package/dist/client.spec.d.ts +1 -0
- package/dist/constants.d.ts +10 -5
- package/dist/helpers.d.ts +2 -0
- package/dist/helpers.spec.d.ts +1 -0
- package/dist/index.d.ts +0 -1
- package/dist/mappers.d.ts +1 -1
- package/dist/mappers.spec.d.ts +1 -0
- package/dist/model.d.ts +2 -0
- package/dist/urls.d.ts +4 -10
- package/package.json +6 -3
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
|
+
```
|
package/dist/api-client.js
CHANGED
|
@@ -1,33 +1,42 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
return `/contents/${
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
u as mapFieldToValue
|
|
40
|
+
O as createClient,
|
|
41
|
+
i as mapFieldListToModel
|
|
33
42
|
};
|
package/dist/api-client.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
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
|
-
|
|
1
|
+
import { Project, Content } from '@content-island/b2b-api-model';
|
|
2
|
+
import { QueryParams } from './model';
|
|
3
|
+
export interface Options {
|
|
2
4
|
accessToken: string;
|
|
3
|
-
|
|
5
|
+
domain?: string;
|
|
6
|
+
apiVersion?: string;
|
|
4
7
|
}
|
|
5
|
-
export interface
|
|
6
|
-
|
|
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
|
|
9
|
-
export {};
|
|
13
|
+
export declare const createClient: (options: Options) => ApiClient;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/constants.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
export declare const
|
|
2
|
-
|
|
3
|
-
|
|
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
|
|
6
|
-
|
|
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 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
CHANGED
package/dist/mappers.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { Field } from '@content-island/b2b-api-model';
|
|
2
|
-
export declare const
|
|
2
|
+
export declare const mapFieldListToModel: <Model>(fields: Field[]) => Model;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/model.d.ts
ADDED
package/dist/urls.d.ts
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
filterList: string[];
|
|
3
|
-
}
|
|
4
|
-
interface ContentParams extends BaseParams {
|
|
5
|
-
id: string;
|
|
6
|
-
}
|
|
1
|
+
import { QueryParams } from './model';
|
|
7
2
|
export declare const API_URLS: {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Content Island - REST API Client",
|
|
5
5
|
"private": false,
|
|
6
6
|
"author": "Lemoncode",
|
|
@@ -23,7 +23,9 @@
|
|
|
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
31
|
"@content-island/b2b-api-model": "^0.2.0"
|
|
@@ -33,6 +35,7 @@
|
|
|
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
|
}
|