@content-island/api-client 0.7.0 → 0.8.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 +8 -147
- package/dist/index.d.ts +54 -34
- package/dist/index.js +48 -39
- package/dist/index.umd.cjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,159 +22,20 @@ interface Post {
|
|
|
22
22
|
```
|
|
23
23
|
|
|
24
24
|
```typescript
|
|
25
|
-
import { createClient
|
|
25
|
+
import { createClient } from '@content-island/api-client';
|
|
26
26
|
|
|
27
27
|
const client = createClient({ accessToken: <your-token>});
|
|
28
28
|
|
|
29
|
-
const
|
|
30
|
-
const englishPosts =
|
|
31
|
-
const spanishPosts =
|
|
29
|
+
const postsWithDefaultLanguage = client.getContentList<Post>({ contentType: 'post'}); // Retrieve the list of contents in the project filtered by content type, for example 'post' in the
|
|
30
|
+
const englishPosts = client.getContentList<Post>({ contentType: 'post', language: 'en'}); // Get english posts
|
|
31
|
+
const spanishPosts = client.getContentList<Post>({ contentType: 'post', language: 'es'}); // Get spanish posts
|
|
32
32
|
|
|
33
33
|
// Or you can retrieve a content by id
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
const spanishPost = mapContentToModel<Post>(content, 'es'); // Map the spanish content to your own model
|
|
34
|
+
const postById = client.getContent<Post>({ id: 'content-id', language: 'es' }); // Retrieve a content by id
|
|
35
|
+
const postBySomeField = client.getContent<Post>({ 'fields.title': 'post-title', language: 'es' }); // Retrieve a content by field value
|
|
37
36
|
|
|
38
37
|
```
|
|
39
38
|
|
|
40
|
-
###
|
|
39
|
+
### Documentation
|
|
41
40
|
|
|
42
|
-
|
|
43
|
-
import { createClient, mapContentToModel } from '@content-island/api-client';
|
|
44
|
-
|
|
45
|
-
const client = createClient({ accessToken: <your-token>});
|
|
46
|
-
|
|
47
|
-
const englishPostContents = client.getContentList({ contentType: 'post', language: 'en'}); // Retrieve the english posts
|
|
48
|
-
const englishPosts = englishPostContents.map(content => mapContentToModel<Post>(content)); // Map content to your own model using the first language available, in this case 'en'
|
|
49
|
-
|
|
50
|
-
// Or you can retrieve contents by multiple languages
|
|
51
|
-
const postContents = client.getContentList({ contentType: 'post', language: { in: ['en', 'es']}}); // Retrieve the english or spanish posts
|
|
52
|
-
|
|
53
|
-
// Or you can retrieve a content by id
|
|
54
|
-
const englishContent = client.getContent('content-id', { language: 'en'}); // Retrieve a content by id
|
|
55
|
-
const englishPost = mapContentToModel<Post>(englishContent); // Map content to your own model using the first language available, in this case 'en'
|
|
56
|
-
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
> Check the [API/queryParams](#queryparams) section for more information about the query parameters.
|
|
60
|
-
|
|
61
|
-
## API
|
|
62
|
-
|
|
63
|
-
### `createClient(options): endpoints`
|
|
64
|
-
|
|
65
|
-
Creates a client instance.
|
|
66
|
-
|
|
67
|
-
#### `options`
|
|
68
|
-
|
|
69
|
-
| name | type | required | description |
|
|
70
|
-
| ------------- | -------- | -------- | ----------------------------------------------------------- |
|
|
71
|
-
| `accessToken` | `string` | `true` | The access token of the project |
|
|
72
|
-
| `domain` | `string` | `false` | The domain of the project. Default: `api.contentisland.net` |
|
|
73
|
-
| `apiVersion` | `string` | `false` | The version of the API. Default: `1.0` |
|
|
74
|
-
|
|
75
|
-
#### `endpoints`
|
|
76
|
-
|
|
77
|
-
##### `getProject()`
|
|
78
|
-
|
|
79
|
-
Retrieves the project details by access token.
|
|
80
|
-
|
|
81
|
-
##### `getContentList(queryParams?)`
|
|
82
|
-
|
|
83
|
-
Retrieves the list of contents in the project.
|
|
84
|
-
|
|
85
|
-
##### `getContent(id, queryParams?)`
|
|
86
|
-
|
|
87
|
-
Retrieves a content by id.
|
|
88
|
-
|
|
89
|
-
#### `queryParams`
|
|
90
|
-
|
|
91
|
-
The query parameters to filter the list of contents.
|
|
92
|
-
|
|
93
|
-
| Key | Value | Description |
|
|
94
|
-
| ------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
95
|
-
| `id` | `Filter` | The id of the content to retrieve. This is useful to retrieve a list of contents by id. For example: `client.getContentList({ id: { in: ['1', '2', '3']})` |
|
|
96
|
-
| `contentType` | `Filter` | The content type to filter the list of contents. For example: `post` |
|
|
97
|
-
| `language` | `Filter` | The language to filter the list of contents. For example: `en` |
|
|
98
|
-
|
|
99
|
-
| Filter type | Description | Example |
|
|
100
|
-
| ------------------ | ------------------------------------------------------------------ | ------------------------------------ |
|
|
101
|
-
| `string` | Filter the content where the param is `equal to` this value | `{ contentType: 'post' }` |
|
|
102
|
-
| `{ in: string[] }` | Filter the content where the param `contains some` of these values | `{ language: { in: ['en', 'es'] } }` |
|
|
103
|
-
|
|
104
|
-
### `mapContentToModel(content, language?)`
|
|
105
|
-
|
|
106
|
-
Maps a list of fields to a model.
|
|
107
|
-
|
|
108
|
-
```typescript
|
|
109
|
-
const content: Content = {
|
|
110
|
-
id: '1',
|
|
111
|
-
contentType: { id: '10', name: 'post' },
|
|
112
|
-
lastUpdate: '2023-10-20T00:00:00.000Z',
|
|
113
|
-
fields: [
|
|
114
|
-
{
|
|
115
|
-
id: '100',
|
|
116
|
-
name: 'title',
|
|
117
|
-
value: 'My title',
|
|
118
|
-
type: 'short-text',
|
|
119
|
-
isArray: false,
|
|
120
|
-
language: 'en',
|
|
121
|
-
},
|
|
122
|
-
{
|
|
123
|
-
id: '200',
|
|
124
|
-
name: 'body',
|
|
125
|
-
value: '# My long text in markdown',
|
|
126
|
-
type: 'long-text',
|
|
127
|
-
isArray: false,
|
|
128
|
-
language: 'en',
|
|
129
|
-
},
|
|
130
|
-
{
|
|
131
|
-
id: '300',
|
|
132
|
-
name: 'order',
|
|
133
|
-
value: 1,
|
|
134
|
-
type: 'number',
|
|
135
|
-
isArray: false,
|
|
136
|
-
language: 'en',
|
|
137
|
-
},
|
|
138
|
-
{
|
|
139
|
-
id: '400',
|
|
140
|
-
name: 'title',
|
|
141
|
-
value: 'Mi titutlo',
|
|
142
|
-
type: 'short-text',
|
|
143
|
-
isArray: false,
|
|
144
|
-
language: 'es',
|
|
145
|
-
},
|
|
146
|
-
{
|
|
147
|
-
id: '500',
|
|
148
|
-
name: 'body',
|
|
149
|
-
value: '# Mi texto largo en markdown',
|
|
150
|
-
type: 'long-text',
|
|
151
|
-
isArray: false,
|
|
152
|
-
language: 'es',
|
|
153
|
-
},
|
|
154
|
-
{
|
|
155
|
-
id: '600',
|
|
156
|
-
name: 'order',
|
|
157
|
-
value: 1,
|
|
158
|
-
type: 'number',
|
|
159
|
-
isArray: false,
|
|
160
|
-
language: 'es',
|
|
161
|
-
},
|
|
162
|
-
],
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
interface Post {
|
|
166
|
-
id: string;
|
|
167
|
-
title: string;
|
|
168
|
-
body: string;
|
|
169
|
-
order: number;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const post = mapContentToModel<Post>(fields); // Get the first language available, in this case 'en'
|
|
173
|
-
console.log(post); // { id: '1', title: 'My title', body: '# My long text in markdown', order: 1 }
|
|
174
|
-
|
|
175
|
-
const englishPost = mapContentToModel<Post>(fields, 'en');
|
|
176
|
-
console.log(englishPost); // { id: '1', title: 'My title', body: '# My long text in markdown', order: 1 }
|
|
177
|
-
|
|
178
|
-
const spanishPost = mapContentToModel<Post>(fields, 'es');
|
|
179
|
-
console.log(spanishPost); // { id: '1', title: 'Mi titulo', body: '# Mi texto largo en markdown', order: 1 }
|
|
180
|
-
```
|
|
41
|
+
For more detailed documentation, please refer to the [Content Island API Client documentation](https://docs.contentisland.net/client-api/overview/).
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
declare type AllowedQueryKeys = keyof Pick<
|
|
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<
|
|
6
|
-
getContent:
|
|
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
|
|
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;
|
|
@@ -25,6 +25,17 @@ export declare interface Content {
|
|
|
25
25
|
fields: Field[];
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
declare interface ContentType extends ApiLookup {
|
|
29
|
+
fields: ContentTypeField[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
declare interface ContentTypeField extends ApiLookup {
|
|
33
|
+
type: FieldType;
|
|
34
|
+
tsType: string;
|
|
35
|
+
isArray: boolean;
|
|
36
|
+
isRequired?: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
28
39
|
export declare const createClient: (options: Options) => ApiClient;
|
|
29
40
|
|
|
30
41
|
export declare interface Field {
|
|
@@ -36,31 +47,34 @@ export declare interface Field {
|
|
|
36
47
|
language: string;
|
|
37
48
|
}
|
|
38
49
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
declare type
|
|
42
|
-
|
|
|
43
|
-
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
gt?: string;
|
|
51
|
-
gte?: string;
|
|
52
|
-
};
|
|
50
|
+
declare type FieldEntityType = `${string}|${string}`;
|
|
51
|
+
|
|
52
|
+
export declare type FieldType =
|
|
53
|
+
| 'short-text'
|
|
54
|
+
| 'long-text'
|
|
55
|
+
| 'number'
|
|
56
|
+
| 'date'
|
|
57
|
+
| 'date-time'
|
|
58
|
+
| 'media'
|
|
59
|
+
| 'boolean'
|
|
60
|
+
| FieldEntityType;
|
|
53
61
|
|
|
54
|
-
export declare const mapContentToModel: <
|
|
62
|
+
export declare const mapContentToModel: <M extends Model<Language> = Model<any> & Record<string, any>, Language = M["language"]>(content: Content, language?: Language) => M;
|
|
55
63
|
|
|
56
64
|
export declare interface Media {
|
|
57
65
|
name: string;
|
|
58
66
|
url: string;
|
|
59
67
|
}
|
|
60
68
|
|
|
69
|
+
export declare type Model<Language = string> = {
|
|
70
|
+
id: string;
|
|
71
|
+
language?: Language;
|
|
72
|
+
};
|
|
73
|
+
|
|
61
74
|
export declare interface Options {
|
|
62
75
|
accessToken: string;
|
|
63
76
|
domain?: string;
|
|
77
|
+
secureProtocol?: boolean;
|
|
64
78
|
apiVersion?: string;
|
|
65
79
|
}
|
|
66
80
|
|
|
@@ -68,17 +82,23 @@ export declare interface Project {
|
|
|
68
82
|
id: string;
|
|
69
83
|
name: string;
|
|
70
84
|
languages: string[];
|
|
71
|
-
contentTypes?:
|
|
85
|
+
contentTypes?: ContentType[];
|
|
72
86
|
}
|
|
73
87
|
|
|
74
|
-
declare type Query<
|
|
75
|
-
id?:
|
|
76
|
-
lastUpdate?:
|
|
77
|
-
language?:
|
|
78
|
-
contentType?:
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
88
|
+
declare type Query<M extends Model = Model> = {
|
|
89
|
+
id?: ClientFilter;
|
|
90
|
+
lastUpdate?: ClientFilter;
|
|
91
|
+
language?: ClientFilter<M['language'] extends undefined ? string : M['language']>;
|
|
92
|
+
contentType?: ClientFilter;
|
|
93
|
+
} & Partial<
|
|
94
|
+
Omit<
|
|
95
|
+
{
|
|
96
|
+
[K in keyof M as `fields.${string & K}`]: ClientFilter;
|
|
97
|
+
},
|
|
98
|
+
'fields.id' | 'fields.language'
|
|
99
|
+
>
|
|
100
|
+
>;
|
|
101
|
+
|
|
102
|
+
export declare type QueryParams<M extends Model = Model & Record<string, any>> = Pick<Query<M>, AllowedQueryKeys<M>>;
|
|
83
103
|
|
|
84
104
|
export { }
|
package/dist/index.js
CHANGED
|
@@ -1,54 +1,63 @@
|
|
|
1
|
-
const
|
|
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
|
-
},
|
|
5
|
+
}, l = {
|
|
5
6
|
SESSION_KEY: "authorization",
|
|
6
7
|
SESSION_TYPE: "Bearer"
|
|
7
|
-
},
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
),
|
|
24
|
+
), g = (t) => t && Object.keys(t).length > 0 ? `?${I(t, "&")}` : "", a = {
|
|
16
25
|
PROJECT: "/project",
|
|
17
|
-
CONTENT_LIST: (
|
|
18
|
-
CONTENT: (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
},
|
|
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
|
-
[
|
|
33
|
+
[l.SESSION_KEY]: `${l.SESSION_TYPE} ${e.accessToken}`
|
|
28
34
|
}
|
|
29
|
-
}).then((
|
|
30
|
-
if (
|
|
31
|
-
return
|
|
35
|
+
}).then((o) => {
|
|
36
|
+
if (o.ok)
|
|
37
|
+
return o.json();
|
|
32
38
|
throw Error(
|
|
33
39
|
JSON.stringify({
|
|
34
|
-
status:
|
|
35
|
-
statusText:
|
|
40
|
+
status: o.status,
|
|
41
|
+
statusText: o.statusText
|
|
36
42
|
})
|
|
37
43
|
);
|
|
38
|
-
}),
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
}
|
|
59
|
+
};
|
|
51
60
|
export {
|
|
52
|
-
|
|
53
|
-
|
|
61
|
+
d as createClient,
|
|
62
|
+
N as mapContentToModel
|
|
54
63
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(s,
|
|
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"})});
|