@dyrected/sdk 0.0.1 → 1.0.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.md +50 -0
- package/README.md +46 -1
- package/dist/index.cjs +187 -465
- package/dist/index.d.cts +118 -13
- package/dist/index.d.ts +118 -13
- package/dist/index.js +186 -465
- package/package.json +2 -2
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PaginatedResult, FileData } from '@dyrected/core';
|
|
2
|
-
export { FileData as Media } from '@dyrected/core';
|
|
2
|
+
export { Block, CollectionConfig, Field, FieldType, GlobalConfig, FileData as Media, PaginatedResult } from '@dyrected/core';
|
|
3
3
|
|
|
4
4
|
interface QueryArgs {
|
|
5
5
|
limit?: number;
|
|
@@ -7,6 +7,7 @@ interface QueryArgs {
|
|
|
7
7
|
depth?: number;
|
|
8
8
|
where?: any;
|
|
9
9
|
sort?: string;
|
|
10
|
+
initialData?: any[];
|
|
10
11
|
}
|
|
11
12
|
declare class QueryBuilder<T = any> {
|
|
12
13
|
private collection;
|
|
@@ -18,43 +19,138 @@ declare class QueryBuilder<T = any> {
|
|
|
18
19
|
limit(limit: number): this;
|
|
19
20
|
page(page: number): this;
|
|
20
21
|
depth(depth: number): this;
|
|
22
|
+
seed(data: T[]): this;
|
|
21
23
|
exec(): Promise<PaginatedResult<T>>;
|
|
22
24
|
then<TResult1 = PaginatedResult<T>, TResult2 = never>(onfulfilled?: ((value: PaginatedResult<T>) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
23
25
|
}
|
|
24
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Structured error thrown by the SDK when the server returns a non-2xx response.
|
|
29
|
+
*/
|
|
30
|
+
declare class DyrectedError extends Error {
|
|
31
|
+
readonly statusCode: number;
|
|
32
|
+
readonly errors: {
|
|
33
|
+
field?: string;
|
|
34
|
+
message: string;
|
|
35
|
+
}[];
|
|
36
|
+
constructor(message: string, statusCode: number, errors?: {
|
|
37
|
+
field?: string;
|
|
38
|
+
message: string;
|
|
39
|
+
}[]);
|
|
40
|
+
}
|
|
25
41
|
interface DyrectedClientConfig {
|
|
26
42
|
baseUrl: string;
|
|
27
43
|
apiKey?: string;
|
|
44
|
+
siteId?: string;
|
|
28
45
|
headers?: Record<string, string>;
|
|
29
46
|
fetch?: typeof fetch;
|
|
47
|
+
/** Default depth for relationship population. Applied to every request unless overridden per-call. */
|
|
48
|
+
defaultDepth?: number;
|
|
30
49
|
}
|
|
31
|
-
|
|
50
|
+
interface BaseSchema {
|
|
51
|
+
collections: Record<string, any>;
|
|
52
|
+
globals: Record<string, any>;
|
|
53
|
+
}
|
|
54
|
+
declare class DyrectedClient<TSchema extends BaseSchema = any> {
|
|
32
55
|
private baseUrl;
|
|
33
56
|
private headers;
|
|
34
57
|
private fetch;
|
|
58
|
+
private defaultDepth;
|
|
35
59
|
constructor(config: DyrectedClientConfig);
|
|
60
|
+
/**
|
|
61
|
+
* Update the Authorization header with a Bearer token.
|
|
62
|
+
* Call this after a successful login.
|
|
63
|
+
*/
|
|
64
|
+
setToken(token: string): void;
|
|
65
|
+
/**
|
|
66
|
+
* Remove the Authorization header.
|
|
67
|
+
* Call this after logout.
|
|
68
|
+
*/
|
|
69
|
+
clearToken(): void;
|
|
36
70
|
getBaseUrl(): string;
|
|
37
71
|
getSchemas(): Promise<{
|
|
38
72
|
collections: any[];
|
|
39
73
|
globals: any[];
|
|
40
74
|
}>;
|
|
41
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Fetch draft data for a specific preview token.
|
|
77
|
+
* Used in "token" preview mode.
|
|
78
|
+
*/
|
|
79
|
+
getPreviewData(token: string): Promise<any>;
|
|
80
|
+
find<K extends keyof TSchema['collections']>(collection: K & string, args?: QueryArgs): Promise<PaginatedResult<TSchema['collections'][K]>>;
|
|
42
81
|
/**
|
|
43
82
|
* Returns a fluent query builder for a collection.
|
|
44
83
|
*/
|
|
45
|
-
collection<
|
|
46
|
-
find: (args?: QueryArgs) => QueryBuilder<
|
|
84
|
+
collection<K extends keyof TSchema['collections']>(slug: K & string): {
|
|
85
|
+
find: (args?: QueryArgs) => QueryBuilder<TSchema["collections"][K]>;
|
|
47
86
|
findOne: (id: string, args?: {
|
|
48
87
|
depth?: number;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
88
|
+
initialData?: TSchema["collections"][K];
|
|
89
|
+
}) => Promise<TSchema["collections"][K]>;
|
|
90
|
+
create: (data: any) => Promise<TSchema["collections"][K]>;
|
|
91
|
+
update: (id: string, data: any) => Promise<TSchema["collections"][K]>;
|
|
52
92
|
delete: (id: string) => Promise<{
|
|
53
93
|
message: string;
|
|
54
94
|
}>;
|
|
95
|
+
/**
|
|
96
|
+
* Upload a file to this collection. Sends as multipart/form-data.
|
|
97
|
+
* @param file - A File or Blob (browser) or Buffer with filename/mimeType (Node.js)
|
|
98
|
+
* @param data - Additional metadata fields to save alongside the file (e.g. alt, caption)
|
|
99
|
+
*/
|
|
100
|
+
upload: (file: File | Blob, data?: Record<string, string>) => Promise<any>;
|
|
101
|
+
/**
|
|
102
|
+
* Log in with email + password. Returns a JWT token and the user document.
|
|
103
|
+
* Call `client.setToken(token)` afterwards to authenticate subsequent requests.
|
|
104
|
+
*/
|
|
105
|
+
login: (email: string, password: string) => Promise<{
|
|
106
|
+
token: string;
|
|
107
|
+
user: TSchema["collections"][K];
|
|
108
|
+
}>;
|
|
109
|
+
/** Log out. Stateless — token must be discarded client-side; call client.clearToken() too. */
|
|
110
|
+
logout: () => Promise<{
|
|
111
|
+
success: boolean;
|
|
112
|
+
}>;
|
|
113
|
+
/** Return the currently authenticated user (requires a token via setToken). */
|
|
114
|
+
me: () => Promise<TSchema["collections"][K]>;
|
|
115
|
+
/** Issue a fresh token for the currently authenticated user. */
|
|
116
|
+
refreshToken: () => Promise<{
|
|
117
|
+
token: string;
|
|
118
|
+
}>;
|
|
119
|
+
/** Check if this auth collection has any users (initialized). */
|
|
120
|
+
isInitialized: () => Promise<{
|
|
121
|
+
initialized: boolean;
|
|
122
|
+
}>;
|
|
123
|
+
/** Register the very first user in an empty auth collection. */
|
|
124
|
+
registerFirstUser: (data: any) => Promise<{
|
|
125
|
+
token: string;
|
|
126
|
+
user: TSchema["collections"][K];
|
|
127
|
+
}>;
|
|
128
|
+
/** Send an invitation email to a new user. Requires authentication. */
|
|
129
|
+
invite: (email: string) => Promise<{
|
|
130
|
+
success: boolean;
|
|
131
|
+
message: string;
|
|
132
|
+
}>;
|
|
133
|
+
/** Accept an invitation and create an account. Returns token + user. */
|
|
134
|
+
acceptInvite: (token: string, password: string, extraFields?: Record<string, any>) => Promise<{
|
|
135
|
+
token: string;
|
|
136
|
+
user: TSchema["collections"][K];
|
|
137
|
+
}>;
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Access a global by its slug with a fluent builder.
|
|
141
|
+
* @example client.global('site-settings').get()
|
|
142
|
+
* @example client.global('site-settings').update({ siteName: 'My Site' })
|
|
143
|
+
*/
|
|
144
|
+
global<K extends keyof TSchema['globals']>(slug: K & string): {
|
|
145
|
+
get: (args?: {
|
|
146
|
+
depth?: number;
|
|
147
|
+
initialData?: TSchema["globals"][K];
|
|
148
|
+
}) => Promise<TSchema["globals"][K]>;
|
|
149
|
+
update: (data: any) => Promise<TSchema["globals"][K]>;
|
|
55
150
|
};
|
|
56
151
|
findOne<T = any>(collection: string, id: string, args?: {
|
|
57
152
|
depth?: number;
|
|
153
|
+
initialData?: T;
|
|
58
154
|
}): Promise<T>;
|
|
59
155
|
create<T = any>(collection: string, data: any): Promise<T>;
|
|
60
156
|
update<T = any>(collection: string, id: string, data: any): Promise<T>;
|
|
@@ -63,15 +159,24 @@ declare class DyrectedClient {
|
|
|
63
159
|
}>;
|
|
64
160
|
getGlobal<T = any>(slug: string, args?: {
|
|
65
161
|
depth?: number;
|
|
162
|
+
initialData?: T;
|
|
66
163
|
}): Promise<T>;
|
|
67
164
|
updateGlobal<T = any>(slug: string, data: any): Promise<T>;
|
|
68
|
-
listMedia(args?: QueryArgs): Promise<PaginatedResult<FileData>>;
|
|
69
|
-
|
|
70
|
-
|
|
165
|
+
listMedia(args?: QueryArgs, collection?: string): Promise<PaginatedResult<FileData>>;
|
|
166
|
+
/** @deprecated Use client.collection('media').upload(file, data) instead */
|
|
167
|
+
uploadMedia(file: File, collection?: string): Promise<FileData>;
|
|
168
|
+
/**
|
|
169
|
+
* Internal upload implementation shared by collection().upload() and uploadMedia().
|
|
170
|
+
*/
|
|
171
|
+
private _upload;
|
|
172
|
+
deleteMedia(id: string, collection?: string): Promise<{
|
|
71
173
|
message: string;
|
|
72
174
|
}>;
|
|
73
175
|
private request;
|
|
74
176
|
}
|
|
75
|
-
declare function createClient
|
|
177
|
+
declare function createClient<TSchema extends {
|
|
178
|
+
collections: any;
|
|
179
|
+
globals: any;
|
|
180
|
+
} = any>(config: DyrectedClientConfig): DyrectedClient<TSchema>;
|
|
76
181
|
|
|
77
|
-
export { DyrectedClient, type DyrectedClientConfig, createClient };
|
|
182
|
+
export { type BaseSchema, DyrectedClient, type DyrectedClientConfig, DyrectedError, createClient };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PaginatedResult, FileData } from '@dyrected/core';
|
|
2
|
-
export { FileData as Media } from '@dyrected/core';
|
|
2
|
+
export { Block, CollectionConfig, Field, FieldType, GlobalConfig, FileData as Media, PaginatedResult } from '@dyrected/core';
|
|
3
3
|
|
|
4
4
|
interface QueryArgs {
|
|
5
5
|
limit?: number;
|
|
@@ -7,6 +7,7 @@ interface QueryArgs {
|
|
|
7
7
|
depth?: number;
|
|
8
8
|
where?: any;
|
|
9
9
|
sort?: string;
|
|
10
|
+
initialData?: any[];
|
|
10
11
|
}
|
|
11
12
|
declare class QueryBuilder<T = any> {
|
|
12
13
|
private collection;
|
|
@@ -18,43 +19,138 @@ declare class QueryBuilder<T = any> {
|
|
|
18
19
|
limit(limit: number): this;
|
|
19
20
|
page(page: number): this;
|
|
20
21
|
depth(depth: number): this;
|
|
22
|
+
seed(data: T[]): this;
|
|
21
23
|
exec(): Promise<PaginatedResult<T>>;
|
|
22
24
|
then<TResult1 = PaginatedResult<T>, TResult2 = never>(onfulfilled?: ((value: PaginatedResult<T>) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
23
25
|
}
|
|
24
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Structured error thrown by the SDK when the server returns a non-2xx response.
|
|
29
|
+
*/
|
|
30
|
+
declare class DyrectedError extends Error {
|
|
31
|
+
readonly statusCode: number;
|
|
32
|
+
readonly errors: {
|
|
33
|
+
field?: string;
|
|
34
|
+
message: string;
|
|
35
|
+
}[];
|
|
36
|
+
constructor(message: string, statusCode: number, errors?: {
|
|
37
|
+
field?: string;
|
|
38
|
+
message: string;
|
|
39
|
+
}[]);
|
|
40
|
+
}
|
|
25
41
|
interface DyrectedClientConfig {
|
|
26
42
|
baseUrl: string;
|
|
27
43
|
apiKey?: string;
|
|
44
|
+
siteId?: string;
|
|
28
45
|
headers?: Record<string, string>;
|
|
29
46
|
fetch?: typeof fetch;
|
|
47
|
+
/** Default depth for relationship population. Applied to every request unless overridden per-call. */
|
|
48
|
+
defaultDepth?: number;
|
|
30
49
|
}
|
|
31
|
-
|
|
50
|
+
interface BaseSchema {
|
|
51
|
+
collections: Record<string, any>;
|
|
52
|
+
globals: Record<string, any>;
|
|
53
|
+
}
|
|
54
|
+
declare class DyrectedClient<TSchema extends BaseSchema = any> {
|
|
32
55
|
private baseUrl;
|
|
33
56
|
private headers;
|
|
34
57
|
private fetch;
|
|
58
|
+
private defaultDepth;
|
|
35
59
|
constructor(config: DyrectedClientConfig);
|
|
60
|
+
/**
|
|
61
|
+
* Update the Authorization header with a Bearer token.
|
|
62
|
+
* Call this after a successful login.
|
|
63
|
+
*/
|
|
64
|
+
setToken(token: string): void;
|
|
65
|
+
/**
|
|
66
|
+
* Remove the Authorization header.
|
|
67
|
+
* Call this after logout.
|
|
68
|
+
*/
|
|
69
|
+
clearToken(): void;
|
|
36
70
|
getBaseUrl(): string;
|
|
37
71
|
getSchemas(): Promise<{
|
|
38
72
|
collections: any[];
|
|
39
73
|
globals: any[];
|
|
40
74
|
}>;
|
|
41
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Fetch draft data for a specific preview token.
|
|
77
|
+
* Used in "token" preview mode.
|
|
78
|
+
*/
|
|
79
|
+
getPreviewData(token: string): Promise<any>;
|
|
80
|
+
find<K extends keyof TSchema['collections']>(collection: K & string, args?: QueryArgs): Promise<PaginatedResult<TSchema['collections'][K]>>;
|
|
42
81
|
/**
|
|
43
82
|
* Returns a fluent query builder for a collection.
|
|
44
83
|
*/
|
|
45
|
-
collection<
|
|
46
|
-
find: (args?: QueryArgs) => QueryBuilder<
|
|
84
|
+
collection<K extends keyof TSchema['collections']>(slug: K & string): {
|
|
85
|
+
find: (args?: QueryArgs) => QueryBuilder<TSchema["collections"][K]>;
|
|
47
86
|
findOne: (id: string, args?: {
|
|
48
87
|
depth?: number;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
88
|
+
initialData?: TSchema["collections"][K];
|
|
89
|
+
}) => Promise<TSchema["collections"][K]>;
|
|
90
|
+
create: (data: any) => Promise<TSchema["collections"][K]>;
|
|
91
|
+
update: (id: string, data: any) => Promise<TSchema["collections"][K]>;
|
|
52
92
|
delete: (id: string) => Promise<{
|
|
53
93
|
message: string;
|
|
54
94
|
}>;
|
|
95
|
+
/**
|
|
96
|
+
* Upload a file to this collection. Sends as multipart/form-data.
|
|
97
|
+
* @param file - A File or Blob (browser) or Buffer with filename/mimeType (Node.js)
|
|
98
|
+
* @param data - Additional metadata fields to save alongside the file (e.g. alt, caption)
|
|
99
|
+
*/
|
|
100
|
+
upload: (file: File | Blob, data?: Record<string, string>) => Promise<any>;
|
|
101
|
+
/**
|
|
102
|
+
* Log in with email + password. Returns a JWT token and the user document.
|
|
103
|
+
* Call `client.setToken(token)` afterwards to authenticate subsequent requests.
|
|
104
|
+
*/
|
|
105
|
+
login: (email: string, password: string) => Promise<{
|
|
106
|
+
token: string;
|
|
107
|
+
user: TSchema["collections"][K];
|
|
108
|
+
}>;
|
|
109
|
+
/** Log out. Stateless — token must be discarded client-side; call client.clearToken() too. */
|
|
110
|
+
logout: () => Promise<{
|
|
111
|
+
success: boolean;
|
|
112
|
+
}>;
|
|
113
|
+
/** Return the currently authenticated user (requires a token via setToken). */
|
|
114
|
+
me: () => Promise<TSchema["collections"][K]>;
|
|
115
|
+
/** Issue a fresh token for the currently authenticated user. */
|
|
116
|
+
refreshToken: () => Promise<{
|
|
117
|
+
token: string;
|
|
118
|
+
}>;
|
|
119
|
+
/** Check if this auth collection has any users (initialized). */
|
|
120
|
+
isInitialized: () => Promise<{
|
|
121
|
+
initialized: boolean;
|
|
122
|
+
}>;
|
|
123
|
+
/** Register the very first user in an empty auth collection. */
|
|
124
|
+
registerFirstUser: (data: any) => Promise<{
|
|
125
|
+
token: string;
|
|
126
|
+
user: TSchema["collections"][K];
|
|
127
|
+
}>;
|
|
128
|
+
/** Send an invitation email to a new user. Requires authentication. */
|
|
129
|
+
invite: (email: string) => Promise<{
|
|
130
|
+
success: boolean;
|
|
131
|
+
message: string;
|
|
132
|
+
}>;
|
|
133
|
+
/** Accept an invitation and create an account. Returns token + user. */
|
|
134
|
+
acceptInvite: (token: string, password: string, extraFields?: Record<string, any>) => Promise<{
|
|
135
|
+
token: string;
|
|
136
|
+
user: TSchema["collections"][K];
|
|
137
|
+
}>;
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Access a global by its slug with a fluent builder.
|
|
141
|
+
* @example client.global('site-settings').get()
|
|
142
|
+
* @example client.global('site-settings').update({ siteName: 'My Site' })
|
|
143
|
+
*/
|
|
144
|
+
global<K extends keyof TSchema['globals']>(slug: K & string): {
|
|
145
|
+
get: (args?: {
|
|
146
|
+
depth?: number;
|
|
147
|
+
initialData?: TSchema["globals"][K];
|
|
148
|
+
}) => Promise<TSchema["globals"][K]>;
|
|
149
|
+
update: (data: any) => Promise<TSchema["globals"][K]>;
|
|
55
150
|
};
|
|
56
151
|
findOne<T = any>(collection: string, id: string, args?: {
|
|
57
152
|
depth?: number;
|
|
153
|
+
initialData?: T;
|
|
58
154
|
}): Promise<T>;
|
|
59
155
|
create<T = any>(collection: string, data: any): Promise<T>;
|
|
60
156
|
update<T = any>(collection: string, id: string, data: any): Promise<T>;
|
|
@@ -63,15 +159,24 @@ declare class DyrectedClient {
|
|
|
63
159
|
}>;
|
|
64
160
|
getGlobal<T = any>(slug: string, args?: {
|
|
65
161
|
depth?: number;
|
|
162
|
+
initialData?: T;
|
|
66
163
|
}): Promise<T>;
|
|
67
164
|
updateGlobal<T = any>(slug: string, data: any): Promise<T>;
|
|
68
|
-
listMedia(args?: QueryArgs): Promise<PaginatedResult<FileData>>;
|
|
69
|
-
|
|
70
|
-
|
|
165
|
+
listMedia(args?: QueryArgs, collection?: string): Promise<PaginatedResult<FileData>>;
|
|
166
|
+
/** @deprecated Use client.collection('media').upload(file, data) instead */
|
|
167
|
+
uploadMedia(file: File, collection?: string): Promise<FileData>;
|
|
168
|
+
/**
|
|
169
|
+
* Internal upload implementation shared by collection().upload() and uploadMedia().
|
|
170
|
+
*/
|
|
171
|
+
private _upload;
|
|
172
|
+
deleteMedia(id: string, collection?: string): Promise<{
|
|
71
173
|
message: string;
|
|
72
174
|
}>;
|
|
73
175
|
private request;
|
|
74
176
|
}
|
|
75
|
-
declare function createClient
|
|
177
|
+
declare function createClient<TSchema extends {
|
|
178
|
+
collections: any;
|
|
179
|
+
globals: any;
|
|
180
|
+
} = any>(config: DyrectedClientConfig): DyrectedClient<TSchema>;
|
|
76
181
|
|
|
77
|
-
export { DyrectedClient, type DyrectedClientConfig, createClient };
|
|
182
|
+
export { type BaseSchema, DyrectedClient, type DyrectedClientConfig, DyrectedError, createClient };
|