@adonis-agora/cms-client 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +2 -2
- package/dist/index.d.ts +50 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +73 -0
- package/dist/index.js.map +1 -0
- package/package.json +30 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Davi Carvalho
|
|
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
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @adonis-agora/cms-client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Part of [`@adonis-agora/cms`](https://github.com/DavideCarvalho/adonis-agora-cms), a config-first CMS for AdonisJS. See the repository for the docs.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export type ContentQuery = {
|
|
2
|
+
where?: Record<string, unknown>;
|
|
3
|
+
sort?: string;
|
|
4
|
+
limit?: number;
|
|
5
|
+
page?: number;
|
|
6
|
+
depth?: number;
|
|
7
|
+
locale?: string;
|
|
8
|
+
select?: string;
|
|
9
|
+
};
|
|
10
|
+
export type ContentDocument = Record<string, unknown> & {
|
|
11
|
+
id: string;
|
|
12
|
+
};
|
|
13
|
+
export type ContentList<T> = {
|
|
14
|
+
docs: T[];
|
|
15
|
+
totalDocs: number;
|
|
16
|
+
limit: number;
|
|
17
|
+
page: number;
|
|
18
|
+
totalPages: number;
|
|
19
|
+
hasNextPage: boolean;
|
|
20
|
+
hasPrevPage: boolean;
|
|
21
|
+
};
|
|
22
|
+
export type ContentClientOptions = {
|
|
23
|
+
baseUrl: string;
|
|
24
|
+
/** App token. The tenant is bound to it server-side — the client never chooses it. */
|
|
25
|
+
token?: string;
|
|
26
|
+
fetch?: typeof fetch;
|
|
27
|
+
headers?: Record<string, string>;
|
|
28
|
+
};
|
|
29
|
+
export declare class ContentClientError extends Error {
|
|
30
|
+
readonly status: number;
|
|
31
|
+
readonly code: string;
|
|
32
|
+
constructor(status: number, body: unknown);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Read-only client for a CMS server.
|
|
36
|
+
*
|
|
37
|
+
* Consumes the public REST surface; writes are intentionally absent because app tokens are
|
|
38
|
+
* read-only unless they carry the `write` ability, and a content client should not.
|
|
39
|
+
*/
|
|
40
|
+
export declare function createContentClient<Collections extends string = string>(options: ContentClientOptions): {
|
|
41
|
+
collection<Slug extends Collections | string = string>(slug: Slug): {
|
|
42
|
+
find<T extends ContentDocument = ContentDocument>(query?: ContentQuery): Promise<ContentList<T>>;
|
|
43
|
+
findByID<T extends ContentDocument = ContentDocument>(id: string, query?: Pick<ContentQuery, 'depth' | 'locale'>): Promise<T>;
|
|
44
|
+
};
|
|
45
|
+
global<Slug extends string = string>(slug: Slug): {
|
|
46
|
+
get<T extends Record<string, unknown> = Record<string, unknown>>(): Promise<T>;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
export declare function toSearch(query: ContentQuery): string;
|
|
50
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GAAG;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvE,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI;IAC3B,IAAI,EAAE,CAAC,EAAE,CAAC;IACV,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,OAAO,CAAC;IACrB,WAAW,EAAE,OAAO,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,sFAAsF;IACtF,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC,CAAC;AAEF,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,YAAY,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAMxC;CACF;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,WAAW,SAAS,MAAM,GAAG,MAAM,EACrE,OAAO,EAAE,oBAAoB;IAgB3B,UAAU,CAAC,IAAI,SAAS,WAAW,GAAG,MAAM,GAAG,MAAM,QAAQ,IAAI;QAE7D,IAAI,CAAC,CAAC,SAAS,eAAe,GAAG,eAAe,UACvC,YAAY,GAClB,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;QAI1B,QAAQ,CAAC,CAAC,SAAS,eAAe,GAAG,eAAe,MAC9C,MAAM,UACH,IAAI,CAAC,YAAY,EAAE,OAAO,GAAG,QAAQ,CAAC,GAC5C,OAAO,CAAC,CAAC,CAAC;;IAMjB,MAAM,CAAC,IAAI,SAAS,MAAM,GAAG,MAAM,QAAQ,IAAI;QAE3C,GAAG,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC;;EAMrF;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,YAAY,GAAG,MAAM,CAsBpD"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export class ContentClientError extends Error {
|
|
2
|
+
status;
|
|
3
|
+
code;
|
|
4
|
+
constructor(status, body) {
|
|
5
|
+
const error = body?.error;
|
|
6
|
+
super(error?.message ?? `Request failed with status ${status}`);
|
|
7
|
+
this.name = 'ContentClientError';
|
|
8
|
+
this.status = status;
|
|
9
|
+
this.code = error?.code ?? 'CMS_HTTP';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Read-only client for a CMS server.
|
|
14
|
+
*
|
|
15
|
+
* Consumes the public REST surface; writes are intentionally absent because app tokens are
|
|
16
|
+
* read-only unless they carry the `write` ability, and a content client should not.
|
|
17
|
+
*/
|
|
18
|
+
export function createContentClient(options) {
|
|
19
|
+
const baseUrl = options.baseUrl.replace(/\/$/, '');
|
|
20
|
+
const fetchImpl = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
21
|
+
async function request(path) {
|
|
22
|
+
const headers = { accept: 'application/json', ...options.headers };
|
|
23
|
+
if (options.token)
|
|
24
|
+
headers.authorization = `Bearer ${options.token}`;
|
|
25
|
+
const response = await fetchImpl(`${baseUrl}${path}`, { headers });
|
|
26
|
+
const payload = await response.json().catch(() => null);
|
|
27
|
+
if (!response.ok)
|
|
28
|
+
throw new ContentClientError(response.status, payload);
|
|
29
|
+
return payload;
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
collection(slug) {
|
|
33
|
+
return {
|
|
34
|
+
find(query = {}) {
|
|
35
|
+
return request(`/collections/${slug}${toSearch(query)}`);
|
|
36
|
+
},
|
|
37
|
+
findByID(id, query = {}) {
|
|
38
|
+
return request(`/collections/${slug}/${id}${toSearch(query)}`);
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
global(slug) {
|
|
43
|
+
return {
|
|
44
|
+
get() {
|
|
45
|
+
return request(`/globals/${slug}`);
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
export function toSearch(query) {
|
|
52
|
+
const params = new URLSearchParams();
|
|
53
|
+
for (const [key, value] of Object.entries(query)) {
|
|
54
|
+
if (value === undefined)
|
|
55
|
+
continue;
|
|
56
|
+
if (key === 'where') {
|
|
57
|
+
for (const [field, condition] of Object.entries(value)) {
|
|
58
|
+
if (condition && typeof condition === 'object' && !Array.isArray(condition)) {
|
|
59
|
+
for (const [operator, operand] of Object.entries(condition)) {
|
|
60
|
+
params.set(`where[${field}][${operator}]`, String(operand));
|
|
61
|
+
}
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
params.set(`where[${field}][equals]`, String(condition));
|
|
65
|
+
}
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
params.set(key, String(value));
|
|
69
|
+
}
|
|
70
|
+
const search = params.toString();
|
|
71
|
+
return search ? `?${search}` : '';
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA8BA,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAClC,MAAM,CAAS;IACf,IAAI,CAAS;IAEtB,YAAY,MAAc,EAAE,IAAa;QACvC,MAAM,KAAK,GAAI,IAAwD,EAAE,KAAK,CAAC;QAC/E,KAAK,CAAC,KAAK,EAAE,OAAO,IAAI,8BAA8B,MAAM,EAAE,CAAC,CAAC;QAChE,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;QACjC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,KAAK,EAAE,IAAI,IAAI,UAAU,CAAC;IACxC,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CACjC,OAA6B;IAE7B,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IACnD,MAAM,SAAS,GAAG,OAAO,CAAC,KAAK,IAAI,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAErE,KAAK,UAAU,OAAO,CAAI,IAAY;QACpC,MAAM,OAAO,GAA2B,EAAE,M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K,CAAC,EAAE,CAAC;QACjD,IAAI,KAAK,KAAK,SAAS;YAAE,SAAS;QAClC,IAAI,GAAG,KAAK,OAAO,EAAE,CAAC;YACpB,KAAK,MAAM,CAAC,KAAK,EAAE,SAAS,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAgC,CAAC,EAAE,CAAC;gBAClF,IAAI,SAAS,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC;oBAC5E,KAAK,MAAM,CAAC,QAAQ,EAAE,OAAO,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,SAAoC,CAAC,EAAE,CAAC;wBACvF,MAAM,CAAC,GAAG,CAAC,SAAS,KAAK,KAAK,QAAQ,GAAG,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;oBAC9D,CAAC;oBACD,SAAS;gBACX,CAAC;gBACD,MAAM,CAAC,GAAG,CAAC,SAAS,KAAK,WAAW,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC;YAC3D,CAAC;YACD,SAAS;QACX,CAAC;QACD,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IACjC,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,EAAE,CAAC;IACjC,OAAO,MAAM,CAAC,CAAC,CAAC,IAAI,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;AACpC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonis-agora/cms-client",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Read-only content client for a @adonis-agora/cms server — token-bound tenant, filter wire format, no Adonis dependency.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist/",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"typescript": "^7.0.2",
|
|
23
|
+
"vitest": "^5.0.5"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=20.6.0"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsc -p tsconfig.json",
|
|
30
|
+
"test": "vitest run --passWithNoTests",
|
|
31
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
32
|
+
}
|
|
6
33
|
}
|