@isardsat/editorial-client 6.6.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/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +15 -0
- package/dist/client.d.ts +2 -0
- package/dist/client.js +115 -0
- package/dist/errors.d.ts +15 -0
- package/dist/errors.js +33 -0
- package/dist/helpers.d.ts +70 -0
- package/dist/helpers.js +51 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/types.d.ts +23 -0
- package/dist/types.js +1 -0
- package/package.json +29 -0
- package/src/client.ts +206 -0
- package/src/errors.ts +34 -0
- package/src/index.ts +17 -0
- package/src/types.ts +54 -0
- package/tsconfig.json +10 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Lobelia Earth
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/client.d.ts
ADDED
package/dist/client.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { EditorialError, EditorialNetworkError, EditorialNotFoundError, EditorialValidationError, } from "./errors.js";
|
|
2
|
+
export function createEditorialClient(baseURL, options = {}) {
|
|
3
|
+
const apiBaseURL = new URL("/api/v1/", baseURL);
|
|
4
|
+
const defaultLocale = options.defaultLocale || "en";
|
|
5
|
+
const fetchFn = options.fetch || globalThis.fetch;
|
|
6
|
+
const defaultHeaders = {
|
|
7
|
+
"Content-Type": "application/json",
|
|
8
|
+
...options.headers,
|
|
9
|
+
};
|
|
10
|
+
const timeout = options.timeout || 30000;
|
|
11
|
+
async function request(endpoint, requestOptions = {}) {
|
|
12
|
+
const url = new URL(endpoint, apiBaseURL);
|
|
13
|
+
const controller = new AbortController();
|
|
14
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
15
|
+
try {
|
|
16
|
+
const response = await fetchFn(url.toString(), {
|
|
17
|
+
...requestOptions,
|
|
18
|
+
headers: {
|
|
19
|
+
...defaultHeaders,
|
|
20
|
+
...requestOptions.headers,
|
|
21
|
+
},
|
|
22
|
+
signal: controller.signal,
|
|
23
|
+
});
|
|
24
|
+
clearTimeout(timeoutId);
|
|
25
|
+
if (!response.ok) {
|
|
26
|
+
const errorMessage = await response
|
|
27
|
+
.text()
|
|
28
|
+
.catch(() => response.statusText);
|
|
29
|
+
if (response.status === 404) {
|
|
30
|
+
throw new EditorialNotFoundError(errorMessage);
|
|
31
|
+
}
|
|
32
|
+
if (response.status === 400) {
|
|
33
|
+
throw new EditorialValidationError(errorMessage);
|
|
34
|
+
}
|
|
35
|
+
throw new EditorialError(errorMessage || `HTTP ${response.status} ${response.statusText}`, response.status, response.statusText);
|
|
36
|
+
}
|
|
37
|
+
return await response.json();
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
clearTimeout(timeoutId);
|
|
41
|
+
if (error instanceof EditorialError) {
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
if (error instanceof DOMException && error.name === "AbortError") {
|
|
45
|
+
throw new EditorialNetworkError("Request timeout");
|
|
46
|
+
}
|
|
47
|
+
throw new EditorialNetworkError(`Network error: ${error instanceof Error ? error.message : "Unknown error"}`, error instanceof Error ? error : undefined);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function buildQueryParams(options = {}) {
|
|
51
|
+
const params = new URLSearchParams();
|
|
52
|
+
if (options.locale && options.locale !== defaultLocale) {
|
|
53
|
+
params.append("lang", options.locale);
|
|
54
|
+
}
|
|
55
|
+
if (options.preview) {
|
|
56
|
+
params.append("preview", "true");
|
|
57
|
+
}
|
|
58
|
+
return params;
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
async getSchema() {
|
|
62
|
+
return request("schema");
|
|
63
|
+
},
|
|
64
|
+
async getConfig() {
|
|
65
|
+
return request("config");
|
|
66
|
+
},
|
|
67
|
+
async getContent(options = {}) {
|
|
68
|
+
const params = buildQueryParams(options);
|
|
69
|
+
const endpoint = params.toString() ? `data?${params}` : "data";
|
|
70
|
+
return request(endpoint);
|
|
71
|
+
},
|
|
72
|
+
async getContentByType(type, options = {}) {
|
|
73
|
+
const params = buildQueryParams(options);
|
|
74
|
+
const endpoint = params.toString()
|
|
75
|
+
? `data/${encodeURIComponent(type)}?${params}`
|
|
76
|
+
: `data/${encodeURIComponent(type)}`;
|
|
77
|
+
const result = await request(endpoint);
|
|
78
|
+
return Object.values(result);
|
|
79
|
+
},
|
|
80
|
+
async getContentIds(type, options = {}) {
|
|
81
|
+
const params = new URLSearchParams();
|
|
82
|
+
if (options.preview) {
|
|
83
|
+
params.append("preview", "true");
|
|
84
|
+
}
|
|
85
|
+
const endpoint = params.toString()
|
|
86
|
+
? `data/${encodeURIComponent(type)}/ids?${params}`
|
|
87
|
+
: `data/${encodeURIComponent(type)}/ids`;
|
|
88
|
+
return request(endpoint);
|
|
89
|
+
},
|
|
90
|
+
async getContentById(type, id, options = {}) {
|
|
91
|
+
const params = buildQueryParams(options);
|
|
92
|
+
const endpoint = params.toString()
|
|
93
|
+
? `data/${encodeURIComponent(type)}/${encodeURIComponent(id)}?${params}`
|
|
94
|
+
: `data/${encodeURIComponent(type)}/${encodeURIComponent(id)}`;
|
|
95
|
+
return request(endpoint);
|
|
96
|
+
},
|
|
97
|
+
async createContent(type, id, data) {
|
|
98
|
+
return request(`data/${encodeURIComponent(type)}/${encodeURIComponent(id)}`, {
|
|
99
|
+
method: "PUT",
|
|
100
|
+
body: JSON.stringify(data),
|
|
101
|
+
});
|
|
102
|
+
},
|
|
103
|
+
async updateContent(type, id, data) {
|
|
104
|
+
return request(`data/${encodeURIComponent(type)}/${encodeURIComponent(id)}`, {
|
|
105
|
+
method: "PATCH",
|
|
106
|
+
body: JSON.stringify(data),
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
async deleteContent(type, id) {
|
|
110
|
+
return request(`data/${encodeURIComponent(type)}/${encodeURIComponent(id)}`, {
|
|
111
|
+
method: "DELETE",
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare class EditorialError extends Error {
|
|
2
|
+
readonly status?: number | undefined;
|
|
3
|
+
readonly statusText?: string | undefined;
|
|
4
|
+
constructor(message: string, status?: number | undefined, statusText?: string | undefined);
|
|
5
|
+
}
|
|
6
|
+
export declare class EditorialNetworkError extends EditorialError {
|
|
7
|
+
readonly cause?: Error | undefined;
|
|
8
|
+
constructor(message: string, cause?: Error | undefined);
|
|
9
|
+
}
|
|
10
|
+
export declare class EditorialNotFoundError extends EditorialError {
|
|
11
|
+
constructor(resource: string, id?: string);
|
|
12
|
+
}
|
|
13
|
+
export declare class EditorialValidationError extends EditorialError {
|
|
14
|
+
constructor(message: string);
|
|
15
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export class EditorialError extends Error {
|
|
2
|
+
status;
|
|
3
|
+
statusText;
|
|
4
|
+
constructor(message, status, statusText) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.status = status;
|
|
7
|
+
this.statusText = statusText;
|
|
8
|
+
this.name = "EditorialError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class EditorialNetworkError extends EditorialError {
|
|
12
|
+
cause;
|
|
13
|
+
constructor(message, cause) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.cause = cause;
|
|
16
|
+
this.name = "EditorialNetworkError";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export class EditorialNotFoundError extends EditorialError {
|
|
20
|
+
constructor(resource, id) {
|
|
21
|
+
const message = id
|
|
22
|
+
? `${resource} with id '${id}' not found`
|
|
23
|
+
: `${resource} not found`;
|
|
24
|
+
super(message, 404, "Not Found");
|
|
25
|
+
this.name = "EditorialNotFoundError";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export class EditorialValidationError extends EditorialError {
|
|
29
|
+
constructor(message) {
|
|
30
|
+
super(message, 400, "Bad Request");
|
|
31
|
+
this.name = "EditorialValidationError";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { EditorialDataItem, EditorialDataType, EditorialData } from "@isardsat/editorial-common";
|
|
2
|
+
import type { EditorialClientInterface } from "./types.js";
|
|
3
|
+
export declare function createEditorialHelpers(client: EditorialClientInterface): {
|
|
4
|
+
getContentIds({ type, locale, preview, }: {
|
|
5
|
+
type: string;
|
|
6
|
+
locale?: string;
|
|
7
|
+
preview?: boolean;
|
|
8
|
+
}): Promise<string[]>;
|
|
9
|
+
getContent<T = import("zod").objectOutputType<{
|
|
10
|
+
id: import("zod").ZodString;
|
|
11
|
+
isDraft: import("zod").ZodDefault<import("zod").ZodBoolean>;
|
|
12
|
+
createdAt: import("zod").ZodDefault<import("zod").ZodString>;
|
|
13
|
+
updatedAt: import("zod").ZodDefault<import("zod").ZodString>;
|
|
14
|
+
}, import("zod").ZodTypeAny, "passthrough">>({ type, id, locale, preview, }: {
|
|
15
|
+
type: string;
|
|
16
|
+
id: string;
|
|
17
|
+
locale?: string;
|
|
18
|
+
preview?: boolean;
|
|
19
|
+
}): Promise<T>;
|
|
20
|
+
getContentByType<T = Record<string, import("zod").objectOutputType<{
|
|
21
|
+
id: import("zod").ZodString;
|
|
22
|
+
isDraft: import("zod").ZodDefault<import("zod").ZodBoolean>;
|
|
23
|
+
createdAt: import("zod").ZodDefault<import("zod").ZodString>;
|
|
24
|
+
updatedAt: import("zod").ZodDefault<import("zod").ZodString>;
|
|
25
|
+
}, import("zod").ZodTypeAny, "passthrough">>>({ type, locale, preview, }: {
|
|
26
|
+
type: string;
|
|
27
|
+
locale?: string;
|
|
28
|
+
preview?: boolean;
|
|
29
|
+
}): Promise<T>;
|
|
30
|
+
getAllContent<T = Record<string, Record<string, import("zod").objectOutputType<{
|
|
31
|
+
id: import("zod").ZodString;
|
|
32
|
+
isDraft: import("zod").ZodDefault<import("zod").ZodBoolean>;
|
|
33
|
+
createdAt: import("zod").ZodDefault<import("zod").ZodString>;
|
|
34
|
+
updatedAt: import("zod").ZodDefault<import("zod").ZodString>;
|
|
35
|
+
}, import("zod").ZodTypeAny, "passthrough">>>>({ locale, preview, }?: {
|
|
36
|
+
locale?: string;
|
|
37
|
+
preview?: boolean;
|
|
38
|
+
}): Promise<T>;
|
|
39
|
+
getSchema(): Promise<Record<string, {
|
|
40
|
+
displayName: string;
|
|
41
|
+
fields: Record<string, import("zod").objectOutputType<{
|
|
42
|
+
type: import("zod").ZodEnum<["string", "boolean", "date", "datetime", "markdown", "number", "color", "select", "url"]>;
|
|
43
|
+
displayName: import("zod").ZodString;
|
|
44
|
+
displayExtra: import("zod").ZodOptional<import("zod").ZodString>;
|
|
45
|
+
placeholder: import("zod").ZodOptional<import("zod").ZodString>;
|
|
46
|
+
isRequired: import("zod").ZodOptional<import("zod").ZodBoolean>;
|
|
47
|
+
showInSummary: import("zod").ZodOptional<import("zod").ZodBoolean>;
|
|
48
|
+
}, import("zod").ZodTypeAny, "passthrough">>;
|
|
49
|
+
singleton?: boolean | undefined;
|
|
50
|
+
}>>;
|
|
51
|
+
getConfig(): Promise<{
|
|
52
|
+
name: string;
|
|
53
|
+
publicUrl: string;
|
|
54
|
+
publicDir: string;
|
|
55
|
+
publicDeletedDir: string;
|
|
56
|
+
filesUrl: string;
|
|
57
|
+
largeFilesUrl: string;
|
|
58
|
+
previewUrl?: string | undefined;
|
|
59
|
+
silent?: boolean | undefined;
|
|
60
|
+
firebase?: {
|
|
61
|
+
apiKey: string;
|
|
62
|
+
authDomain: string;
|
|
63
|
+
databaseURL: string;
|
|
64
|
+
projectId: string;
|
|
65
|
+
storageBucket: string;
|
|
66
|
+
messagingSenderId: string;
|
|
67
|
+
dbUsersPath: string;
|
|
68
|
+
} | undefined;
|
|
69
|
+
}>;
|
|
70
|
+
};
|
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const DEFAULT_LANG = "en";
|
|
2
|
+
export function createEditorialHelpers(client) {
|
|
3
|
+
return {
|
|
4
|
+
async getContentIds({ type, locale, preview, }) {
|
|
5
|
+
const options = {};
|
|
6
|
+
if (locale && locale !== DEFAULT_LANG) {
|
|
7
|
+
options.locale = locale;
|
|
8
|
+
}
|
|
9
|
+
if (preview) {
|
|
10
|
+
options.preview = true;
|
|
11
|
+
}
|
|
12
|
+
return client.getContentIds(type, options);
|
|
13
|
+
},
|
|
14
|
+
async getContent({ type, id, locale, preview, }) {
|
|
15
|
+
const options = {};
|
|
16
|
+
if (locale && locale !== DEFAULT_LANG) {
|
|
17
|
+
options.locale = locale;
|
|
18
|
+
}
|
|
19
|
+
if (preview) {
|
|
20
|
+
options.preview = true;
|
|
21
|
+
}
|
|
22
|
+
return client.getContentById(type, id, options);
|
|
23
|
+
},
|
|
24
|
+
async getContentByType({ type, locale, preview, }) {
|
|
25
|
+
const options = {};
|
|
26
|
+
if (locale && locale !== DEFAULT_LANG) {
|
|
27
|
+
options.locale = locale;
|
|
28
|
+
}
|
|
29
|
+
if (preview) {
|
|
30
|
+
options.preview = true;
|
|
31
|
+
}
|
|
32
|
+
return client.getContentByType(type, options);
|
|
33
|
+
},
|
|
34
|
+
async getAllContent({ locale, preview, } = {}) {
|
|
35
|
+
const options = {};
|
|
36
|
+
if (locale && locale !== DEFAULT_LANG) {
|
|
37
|
+
options.locale = locale;
|
|
38
|
+
}
|
|
39
|
+
if (preview) {
|
|
40
|
+
options.preview = true;
|
|
41
|
+
}
|
|
42
|
+
return client.getContent(options);
|
|
43
|
+
},
|
|
44
|
+
async getSchema() {
|
|
45
|
+
return client.getSchema();
|
|
46
|
+
},
|
|
47
|
+
async getConfig() {
|
|
48
|
+
return client.getConfig();
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { createEditorialClient } from "./client.js";
|
|
2
|
+
export { EditorialError, EditorialNetworkError, EditorialNotFoundError, EditorialValidationError, } from "./errors.js";
|
|
3
|
+
export type { ClientOptions, EditorialClientInterface, EditorialConfig, EditorialData, EditorialDataItem, EditorialDataType, EditorialSchema, GetContentOptions, } from "./types.js";
|
package/dist/index.js
ADDED
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { EditorialConfig, EditorialData, EditorialDataItem, EditorialDataType, EditorialSchema } from "@isardsat/editorial-common";
|
|
2
|
+
export interface ClientOptions {
|
|
3
|
+
fetch?: typeof globalThis.fetch;
|
|
4
|
+
headers?: Record<string, string>;
|
|
5
|
+
defaultLocale?: string;
|
|
6
|
+
timeout?: number;
|
|
7
|
+
}
|
|
8
|
+
export interface GetContentOptions {
|
|
9
|
+
locale?: string;
|
|
10
|
+
preview?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface EditorialClientInterface {
|
|
13
|
+
getSchema(): Promise<EditorialSchema>;
|
|
14
|
+
getConfig(): Promise<EditorialConfig>;
|
|
15
|
+
getContent<T = EditorialData>(options?: GetContentOptions): Promise<T>;
|
|
16
|
+
getContentByType<T = EditorialDataType>(type: string, options?: GetContentOptions): Promise<T[]>;
|
|
17
|
+
getContentIds(type: string, options?: GetContentOptions): Promise<string[]>;
|
|
18
|
+
getContentById<T = EditorialDataItem>(type: string, id: string, options?: GetContentOptions): Promise<T>;
|
|
19
|
+
createContent<T = EditorialDataItem>(type: string, id: string, data: T): Promise<T>;
|
|
20
|
+
updateContent<T = EditorialDataItem>(type: string, id: string, data: Partial<T>): Promise<T>;
|
|
21
|
+
deleteContent(type: string, id: string): Promise<boolean>;
|
|
22
|
+
}
|
|
23
|
+
export { EditorialConfig, EditorialData, EditorialDataItem, EditorialDataType, EditorialSchema, };
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@isardsat/editorial-client",
|
|
3
|
+
"version": "6.6.1",
|
|
4
|
+
"description": "JavaScript client for the Editorial CMS API",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"@isardsat/editorial-common": "6.6.1"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@tsconfig/node22": "^22.0.0",
|
|
13
|
+
"typescript": "5.7.3"
|
|
14
|
+
},
|
|
15
|
+
"volta": {
|
|
16
|
+
"extends": "../../package.json"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"editorial",
|
|
20
|
+
"cms",
|
|
21
|
+
"api",
|
|
22
|
+
"client",
|
|
23
|
+
"typescript"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"compile": "tsc",
|
|
27
|
+
"compile:watch": "tsc --watch"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EditorialConfig,
|
|
3
|
+
EditorialData,
|
|
4
|
+
EditorialDataItem,
|
|
5
|
+
EditorialDataType,
|
|
6
|
+
EditorialSchema,
|
|
7
|
+
} from "@isardsat/editorial-common";
|
|
8
|
+
import {
|
|
9
|
+
EditorialError,
|
|
10
|
+
EditorialNetworkError,
|
|
11
|
+
EditorialNotFoundError,
|
|
12
|
+
EditorialValidationError,
|
|
13
|
+
} from "./errors.js";
|
|
14
|
+
import type {
|
|
15
|
+
ClientOptions,
|
|
16
|
+
EditorialClientInterface,
|
|
17
|
+
GetContentOptions,
|
|
18
|
+
} from "./types.js";
|
|
19
|
+
|
|
20
|
+
export function createEditorialClient(
|
|
21
|
+
baseURL: string,
|
|
22
|
+
options: ClientOptions = {}
|
|
23
|
+
): EditorialClientInterface {
|
|
24
|
+
const apiBaseURL = new URL("/api/v1/", baseURL);
|
|
25
|
+
const defaultLocale = options.defaultLocale || "en";
|
|
26
|
+
const fetchFn = options.fetch || globalThis.fetch;
|
|
27
|
+
const defaultHeaders = {
|
|
28
|
+
"Content-Type": "application/json",
|
|
29
|
+
...options.headers,
|
|
30
|
+
};
|
|
31
|
+
const timeout = options.timeout || 30000;
|
|
32
|
+
|
|
33
|
+
async function request<T>(
|
|
34
|
+
endpoint: string,
|
|
35
|
+
requestOptions: RequestInit = {}
|
|
36
|
+
): Promise<T> {
|
|
37
|
+
const url = new URL(endpoint, apiBaseURL);
|
|
38
|
+
|
|
39
|
+
const controller = new AbortController();
|
|
40
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
const response = await fetchFn(url.toString(), {
|
|
44
|
+
...requestOptions,
|
|
45
|
+
headers: {
|
|
46
|
+
...defaultHeaders,
|
|
47
|
+
...requestOptions.headers,
|
|
48
|
+
},
|
|
49
|
+
signal: controller.signal,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
clearTimeout(timeoutId);
|
|
53
|
+
|
|
54
|
+
if (!response.ok) {
|
|
55
|
+
const errorMessage = await response
|
|
56
|
+
.text()
|
|
57
|
+
.catch(() => response.statusText);
|
|
58
|
+
|
|
59
|
+
if (response.status === 404) {
|
|
60
|
+
throw new EditorialNotFoundError(errorMessage);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (response.status === 400) {
|
|
64
|
+
throw new EditorialValidationError(errorMessage);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
throw new EditorialError(
|
|
68
|
+
errorMessage || `HTTP ${response.status} ${response.statusText}`,
|
|
69
|
+
response.status,
|
|
70
|
+
response.statusText
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return await response.json();
|
|
75
|
+
} catch (error) {
|
|
76
|
+
clearTimeout(timeoutId);
|
|
77
|
+
|
|
78
|
+
if (error instanceof EditorialError) {
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (error instanceof DOMException && error.name === "AbortError") {
|
|
83
|
+
throw new EditorialNetworkError("Request timeout");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
throw new EditorialNetworkError(
|
|
87
|
+
`Network error: ${
|
|
88
|
+
error instanceof Error ? error.message : "Unknown error"
|
|
89
|
+
}`,
|
|
90
|
+
error instanceof Error ? error : undefined
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function buildQueryParams(options: GetContentOptions = {}): URLSearchParams {
|
|
96
|
+
const params = new URLSearchParams();
|
|
97
|
+
|
|
98
|
+
if (options.locale && options.locale !== defaultLocale) {
|
|
99
|
+
params.append("lang", options.locale);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (options.preview) {
|
|
103
|
+
params.append("preview", "true");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return params;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
async getSchema(): Promise<EditorialSchema> {
|
|
111
|
+
return request<EditorialSchema>("schema");
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
async getConfig(): Promise<EditorialConfig> {
|
|
115
|
+
return request<EditorialConfig>("config");
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
async getContent<T = EditorialData>(
|
|
119
|
+
options: GetContentOptions = {}
|
|
120
|
+
): Promise<T> {
|
|
121
|
+
const params = buildQueryParams(options);
|
|
122
|
+
const endpoint = params.toString() ? `data?${params}` : "data";
|
|
123
|
+
|
|
124
|
+
return request<T>(endpoint);
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
async getContentByType<T = EditorialDataType>(
|
|
128
|
+
type: string,
|
|
129
|
+
options: GetContentOptions = {}
|
|
130
|
+
): Promise<T[]> {
|
|
131
|
+
const params = buildQueryParams(options);
|
|
132
|
+
const endpoint = params.toString()
|
|
133
|
+
? `data/${encodeURIComponent(type)}?${params}`
|
|
134
|
+
: `data/${encodeURIComponent(type)}`;
|
|
135
|
+
|
|
136
|
+
const result = await request<Record<string, T>>(endpoint);
|
|
137
|
+
return Object.values(result) as T[];
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
async getContentIds(
|
|
141
|
+
type: string,
|
|
142
|
+
options: GetContentOptions = {}
|
|
143
|
+
): Promise<string[]> {
|
|
144
|
+
const params = new URLSearchParams();
|
|
145
|
+
if (options.preview) {
|
|
146
|
+
params.append("preview", "true");
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const endpoint = params.toString()
|
|
150
|
+
? `data/${encodeURIComponent(type)}/ids?${params}`
|
|
151
|
+
: `data/${encodeURIComponent(type)}/ids`;
|
|
152
|
+
|
|
153
|
+
return request<string[]>(endpoint);
|
|
154
|
+
},
|
|
155
|
+
|
|
156
|
+
async getContentById<T = EditorialDataItem>(
|
|
157
|
+
type: string,
|
|
158
|
+
id: string,
|
|
159
|
+
options: GetContentOptions = {}
|
|
160
|
+
): Promise<T> {
|
|
161
|
+
const params = buildQueryParams(options);
|
|
162
|
+
const endpoint = params.toString()
|
|
163
|
+
? `data/${encodeURIComponent(type)}/${encodeURIComponent(id)}?${params}`
|
|
164
|
+
: `data/${encodeURIComponent(type)}/${encodeURIComponent(id)}`;
|
|
165
|
+
|
|
166
|
+
return request<T>(endpoint);
|
|
167
|
+
},
|
|
168
|
+
|
|
169
|
+
async createContent<T = EditorialDataItem>(
|
|
170
|
+
type: string,
|
|
171
|
+
id: string,
|
|
172
|
+
data: T
|
|
173
|
+
): Promise<T> {
|
|
174
|
+
return request<T>(
|
|
175
|
+
`data/${encodeURIComponent(type)}/${encodeURIComponent(id)}`,
|
|
176
|
+
{
|
|
177
|
+
method: "PUT",
|
|
178
|
+
body: JSON.stringify(data),
|
|
179
|
+
}
|
|
180
|
+
);
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
async updateContent<T = EditorialDataItem>(
|
|
184
|
+
type: string,
|
|
185
|
+
id: string,
|
|
186
|
+
data: Partial<T>
|
|
187
|
+
): Promise<T> {
|
|
188
|
+
return request<T>(
|
|
189
|
+
`data/${encodeURIComponent(type)}/${encodeURIComponent(id)}`,
|
|
190
|
+
{
|
|
191
|
+
method: "PATCH",
|
|
192
|
+
body: JSON.stringify(data),
|
|
193
|
+
}
|
|
194
|
+
);
|
|
195
|
+
},
|
|
196
|
+
|
|
197
|
+
async deleteContent(type: string, id: string): Promise<boolean> {
|
|
198
|
+
return request<boolean>(
|
|
199
|
+
`data/${encodeURIComponent(type)}/${encodeURIComponent(id)}`,
|
|
200
|
+
{
|
|
201
|
+
method: "DELETE",
|
|
202
|
+
}
|
|
203
|
+
);
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export class EditorialError extends Error {
|
|
2
|
+
constructor(
|
|
3
|
+
message: string,
|
|
4
|
+
public readonly status?: number,
|
|
5
|
+
public readonly statusText?: string
|
|
6
|
+
) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "EditorialError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class EditorialNetworkError extends EditorialError {
|
|
13
|
+
constructor(message: string, public readonly cause?: Error) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = "EditorialNetworkError";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class EditorialNotFoundError extends EditorialError {
|
|
20
|
+
constructor(resource: string, id?: string) {
|
|
21
|
+
const message = id
|
|
22
|
+
? `${resource} with id '${id}' not found`
|
|
23
|
+
: `${resource} not found`;
|
|
24
|
+
super(message, 404, "Not Found");
|
|
25
|
+
this.name = "EditorialNotFoundError";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class EditorialValidationError extends EditorialError {
|
|
30
|
+
constructor(message: string) {
|
|
31
|
+
super(message, 400, "Bad Request");
|
|
32
|
+
this.name = "EditorialValidationError";
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { createEditorialClient } from "./client.js";
|
|
2
|
+
export {
|
|
3
|
+
EditorialError,
|
|
4
|
+
EditorialNetworkError,
|
|
5
|
+
EditorialNotFoundError,
|
|
6
|
+
EditorialValidationError,
|
|
7
|
+
} from "./errors.js";
|
|
8
|
+
export type {
|
|
9
|
+
ClientOptions,
|
|
10
|
+
EditorialClientInterface,
|
|
11
|
+
EditorialConfig,
|
|
12
|
+
EditorialData,
|
|
13
|
+
EditorialDataItem,
|
|
14
|
+
EditorialDataType,
|
|
15
|
+
EditorialSchema,
|
|
16
|
+
GetContentOptions,
|
|
17
|
+
} from "./types.js";
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EditorialConfig,
|
|
3
|
+
EditorialData,
|
|
4
|
+
EditorialDataItem,
|
|
5
|
+
EditorialDataType,
|
|
6
|
+
EditorialSchema,
|
|
7
|
+
} from "@isardsat/editorial-common";
|
|
8
|
+
|
|
9
|
+
export interface ClientOptions {
|
|
10
|
+
fetch?: typeof globalThis.fetch;
|
|
11
|
+
headers?: Record<string, string>;
|
|
12
|
+
defaultLocale?: string;
|
|
13
|
+
timeout?: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface GetContentOptions {
|
|
17
|
+
locale?: string;
|
|
18
|
+
preview?: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface EditorialClientInterface {
|
|
22
|
+
getSchema(): Promise<EditorialSchema>;
|
|
23
|
+
getConfig(): Promise<EditorialConfig>;
|
|
24
|
+
getContent<T = EditorialData>(options?: GetContentOptions): Promise<T>;
|
|
25
|
+
getContentByType<T = EditorialDataType>(
|
|
26
|
+
type: string,
|
|
27
|
+
options?: GetContentOptions
|
|
28
|
+
): Promise<T[]>;
|
|
29
|
+
getContentIds(type: string, options?: GetContentOptions): Promise<string[]>;
|
|
30
|
+
getContentById<T = EditorialDataItem>(
|
|
31
|
+
type: string,
|
|
32
|
+
id: string,
|
|
33
|
+
options?: GetContentOptions
|
|
34
|
+
): Promise<T>;
|
|
35
|
+
createContent<T = EditorialDataItem>(
|
|
36
|
+
type: string,
|
|
37
|
+
id: string,
|
|
38
|
+
data: T
|
|
39
|
+
): Promise<T>;
|
|
40
|
+
updateContent<T = EditorialDataItem>(
|
|
41
|
+
type: string,
|
|
42
|
+
id: string,
|
|
43
|
+
data: Partial<T>
|
|
44
|
+
): Promise<T>;
|
|
45
|
+
deleteContent(type: string, id: string): Promise<boolean>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export {
|
|
49
|
+
EditorialConfig,
|
|
50
|
+
EditorialData,
|
|
51
|
+
EditorialDataItem,
|
|
52
|
+
EditorialDataType,
|
|
53
|
+
EditorialSchema,
|
|
54
|
+
};
|