@tanglemedia/svelte-starter-directus-api 7.0.0 → 8.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/dist/adapter/api-adapter.d.ts +40 -25
- package/dist/adapter/api-adapter.js +101 -68
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/provider/api-adapter.provider.d.ts +4 -4
- package/dist/provider/api-references-functions.provider.d.ts +10 -1
- package/dist/provider/api-references-functions.provider.js +18 -1
- package/dist/provider/directus.factory.d.ts +2 -2
- package/dist/services/auth.d.ts +3 -2
- package/dist/services/auth.js +13 -5
- package/dist/services/register.js +1 -1
- package/dist/types/adapter.types.d.ts +5 -3
- package/dist/types/adapter.types.js +1 -1
- package/dist/utils/directus-error.d.ts +6 -0
- package/dist/utils/directus-error.js +42 -0
- package/package.json +17 -20
- package/src/adapter/api-adapter.ts +214 -130
- package/src/index.ts +1 -0
- package/src/provider/api-adapter.provider.ts +11 -3
- package/src/provider/api-references-functions.provider.ts +263 -1
- package/src/provider/directus.factory.ts +7 -7
- package/src/services/auth.ts +21 -11
- package/src/services/register.ts +1 -1
- package/src/types/adapter.types.ts +26 -9
- package/src/utils/directus-error.ts +75 -0
|
@@ -1 +1,263 @@
|
|
|
1
|
-
|
|
1
|
+
// Client setup
|
|
2
|
+
export { authentication, createDirectus, memoryStorage, rest, staticToken } from '@directus/sdk';
|
|
3
|
+
|
|
4
|
+
// Auth commands
|
|
5
|
+
export {
|
|
6
|
+
login,
|
|
7
|
+
logout,
|
|
8
|
+
passwordRequest,
|
|
9
|
+
passwordReset,
|
|
10
|
+
readProviders,
|
|
11
|
+
refresh,
|
|
12
|
+
} from '@directus/sdk';
|
|
13
|
+
|
|
14
|
+
// Create commands
|
|
15
|
+
export {
|
|
16
|
+
createCollection,
|
|
17
|
+
createComment,
|
|
18
|
+
createComments,
|
|
19
|
+
createContentVersion,
|
|
20
|
+
createContentVersions,
|
|
21
|
+
createDashboard,
|
|
22
|
+
createDashboards,
|
|
23
|
+
createField,
|
|
24
|
+
createFlow,
|
|
25
|
+
createFlows,
|
|
26
|
+
createFolder,
|
|
27
|
+
createFolders,
|
|
28
|
+
createItem,
|
|
29
|
+
createItems,
|
|
30
|
+
createNotification,
|
|
31
|
+
createNotifications,
|
|
32
|
+
createOperation,
|
|
33
|
+
createOperations,
|
|
34
|
+
createPanel,
|
|
35
|
+
createPanels,
|
|
36
|
+
createPermission,
|
|
37
|
+
createPermissions,
|
|
38
|
+
createPolicies,
|
|
39
|
+
createPolicy,
|
|
40
|
+
createPreset,
|
|
41
|
+
createPresets,
|
|
42
|
+
createRelation,
|
|
43
|
+
createRole,
|
|
44
|
+
createRoles,
|
|
45
|
+
createShare,
|
|
46
|
+
createShares,
|
|
47
|
+
createTranslation,
|
|
48
|
+
createTranslations,
|
|
49
|
+
createUser,
|
|
50
|
+
createUsers,
|
|
51
|
+
importFile,
|
|
52
|
+
uploadFiles,
|
|
53
|
+
} from '@directus/sdk';
|
|
54
|
+
|
|
55
|
+
// Read commands
|
|
56
|
+
export {
|
|
57
|
+
aggregate,
|
|
58
|
+
downloadFilesZip,
|
|
59
|
+
downloadFolderZip,
|
|
60
|
+
readActivities,
|
|
61
|
+
readActivity,
|
|
62
|
+
readAssetArrayBuffer,
|
|
63
|
+
readAssetBlob,
|
|
64
|
+
readAssetRaw,
|
|
65
|
+
readCollection,
|
|
66
|
+
readCollections,
|
|
67
|
+
readComment,
|
|
68
|
+
readComments,
|
|
69
|
+
readContentVersion,
|
|
70
|
+
readContentVersions,
|
|
71
|
+
readDashboard,
|
|
72
|
+
readDashboards,
|
|
73
|
+
readExtensions,
|
|
74
|
+
readField,
|
|
75
|
+
readFields,
|
|
76
|
+
readFieldsByCollection,
|
|
77
|
+
readFile,
|
|
78
|
+
readFiles,
|
|
79
|
+
readFlow,
|
|
80
|
+
readFlows,
|
|
81
|
+
readFolder,
|
|
82
|
+
readFolders,
|
|
83
|
+
readGraphqlSdl,
|
|
84
|
+
readItem,
|
|
85
|
+
readItemPermissions,
|
|
86
|
+
readItems,
|
|
87
|
+
readMe,
|
|
88
|
+
readNotification,
|
|
89
|
+
readNotifications,
|
|
90
|
+
readOpenApiSpec,
|
|
91
|
+
readOperation,
|
|
92
|
+
readOperations,
|
|
93
|
+
readPanel,
|
|
94
|
+
readPanels,
|
|
95
|
+
readPermission,
|
|
96
|
+
readPermissions,
|
|
97
|
+
readPolicies,
|
|
98
|
+
readPolicy,
|
|
99
|
+
readPolicyGlobals,
|
|
100
|
+
readPreset,
|
|
101
|
+
readPresets,
|
|
102
|
+
readRelation,
|
|
103
|
+
readRelationByCollection,
|
|
104
|
+
readRelations,
|
|
105
|
+
readRevision,
|
|
106
|
+
readRevisions,
|
|
107
|
+
readRole,
|
|
108
|
+
readRoles,
|
|
109
|
+
readRolesMe,
|
|
110
|
+
readSettings,
|
|
111
|
+
readShare,
|
|
112
|
+
readShareInfo,
|
|
113
|
+
readShares,
|
|
114
|
+
readSingleton,
|
|
115
|
+
readTranslation,
|
|
116
|
+
readTranslations,
|
|
117
|
+
readUser,
|
|
118
|
+
readUserPermissions,
|
|
119
|
+
readUsers,
|
|
120
|
+
} from '@directus/sdk';
|
|
121
|
+
|
|
122
|
+
// Update commands
|
|
123
|
+
export {
|
|
124
|
+
updateCollection,
|
|
125
|
+
updateCollectionsBatch,
|
|
126
|
+
updateComment,
|
|
127
|
+
updateComments,
|
|
128
|
+
updateCommentsBatch,
|
|
129
|
+
updateContentVersion,
|
|
130
|
+
updateContentVersions,
|
|
131
|
+
updateContentVersionsBatch,
|
|
132
|
+
updateDashboard,
|
|
133
|
+
updateDashboards,
|
|
134
|
+
updateDashboardsBatch,
|
|
135
|
+
updateExtension,
|
|
136
|
+
updateField,
|
|
137
|
+
updateFields,
|
|
138
|
+
updateFile,
|
|
139
|
+
updateFiles,
|
|
140
|
+
updateFilesBatch,
|
|
141
|
+
updateFlow,
|
|
142
|
+
updateFlows,
|
|
143
|
+
updateFlowsBatch,
|
|
144
|
+
updateFolder,
|
|
145
|
+
updateFolders,
|
|
146
|
+
updateFoldersBatch,
|
|
147
|
+
updateItem,
|
|
148
|
+
updateItems,
|
|
149
|
+
updateItemsBatch,
|
|
150
|
+
updateMe,
|
|
151
|
+
updateNotification,
|
|
152
|
+
updateNotifications,
|
|
153
|
+
updateNotificationsBatch,
|
|
154
|
+
updateOperation,
|
|
155
|
+
updateOperations,
|
|
156
|
+
updateOperationsBatch,
|
|
157
|
+
updatePanel,
|
|
158
|
+
updatePanels,
|
|
159
|
+
updatePanelsBatch,
|
|
160
|
+
updatePermission,
|
|
161
|
+
updatePermissions,
|
|
162
|
+
updatePermissionsBatch,
|
|
163
|
+
updatePolicies,
|
|
164
|
+
updatePoliciesBatch,
|
|
165
|
+
updatePolicy,
|
|
166
|
+
updatePreset,
|
|
167
|
+
updatePresets,
|
|
168
|
+
updatePresetsBatch,
|
|
169
|
+
updateRelation,
|
|
170
|
+
updateRole,
|
|
171
|
+
updateRoles,
|
|
172
|
+
updateRolesBatch,
|
|
173
|
+
updateSettings,
|
|
174
|
+
updateShare,
|
|
175
|
+
updateShares,
|
|
176
|
+
updateSharesBatch,
|
|
177
|
+
updateSingleton,
|
|
178
|
+
updateTranslation,
|
|
179
|
+
updateTranslations,
|
|
180
|
+
updateTranslationsBatch,
|
|
181
|
+
updateUser,
|
|
182
|
+
updateUsers,
|
|
183
|
+
updateUsersBatch,
|
|
184
|
+
} from '@directus/sdk';
|
|
185
|
+
|
|
186
|
+
// Delete commands
|
|
187
|
+
export {
|
|
188
|
+
deleteCollection,
|
|
189
|
+
deleteComment,
|
|
190
|
+
deleteComments,
|
|
191
|
+
deleteContentVersion,
|
|
192
|
+
deleteContentVersions,
|
|
193
|
+
deleteDashboard,
|
|
194
|
+
deleteDashboards,
|
|
195
|
+
deleteField,
|
|
196
|
+
deleteFile,
|
|
197
|
+
deleteFiles,
|
|
198
|
+
deleteFlow,
|
|
199
|
+
deleteFlows,
|
|
200
|
+
deleteFolder,
|
|
201
|
+
deleteFolders,
|
|
202
|
+
deleteItem,
|
|
203
|
+
deleteItems,
|
|
204
|
+
deleteNotification,
|
|
205
|
+
deleteNotifications,
|
|
206
|
+
deleteOperation,
|
|
207
|
+
deleteOperations,
|
|
208
|
+
deletePanel,
|
|
209
|
+
deletePanels,
|
|
210
|
+
deletePermission,
|
|
211
|
+
deletePermissions,
|
|
212
|
+
deletePolicies,
|
|
213
|
+
deletePolicy,
|
|
214
|
+
deletePreset,
|
|
215
|
+
deletePresets,
|
|
216
|
+
deleteRelation,
|
|
217
|
+
deleteRole,
|
|
218
|
+
deleteRoles,
|
|
219
|
+
deleteShare,
|
|
220
|
+
deleteShares,
|
|
221
|
+
deleteTranslation,
|
|
222
|
+
deleteTranslations,
|
|
223
|
+
deleteUser,
|
|
224
|
+
deleteUsers,
|
|
225
|
+
} from '@directus/sdk';
|
|
226
|
+
|
|
227
|
+
// Schema commands
|
|
228
|
+
export { schemaApply, schemaDiff, schemaSnapshot } from '@directus/sdk';
|
|
229
|
+
|
|
230
|
+
// Server commands
|
|
231
|
+
export { serverHealth, serverInfo, serverPing } from '@directus/sdk';
|
|
232
|
+
|
|
233
|
+
// Utility functions
|
|
234
|
+
export {
|
|
235
|
+
acceptUserInvite,
|
|
236
|
+
authenticateShare,
|
|
237
|
+
clearCache,
|
|
238
|
+
compareContentVersion,
|
|
239
|
+
customEndpoint,
|
|
240
|
+
disableTwoFactor,
|
|
241
|
+
enableTwoFactor,
|
|
242
|
+
generateHash,
|
|
243
|
+
generateTwoFactorSecret,
|
|
244
|
+
inviteShare,
|
|
245
|
+
inviteUser,
|
|
246
|
+
isDirectusError,
|
|
247
|
+
promoteContentVersion,
|
|
248
|
+
randomString,
|
|
249
|
+
registerUser,
|
|
250
|
+
registerUserVerify,
|
|
251
|
+
saveToContentVersion,
|
|
252
|
+
triggerFlow,
|
|
253
|
+
utilitySort,
|
|
254
|
+
utilsExport,
|
|
255
|
+
utilsImport,
|
|
256
|
+
verifyHash,
|
|
257
|
+
withOptions,
|
|
258
|
+
withSearch,
|
|
259
|
+
withToken,
|
|
260
|
+
} from '@directus/sdk';
|
|
261
|
+
|
|
262
|
+
// Types
|
|
263
|
+
export type { DirectusClient, RestClient, RestCommand } from '@directus/sdk';
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
type ClientOptions
|
|
9
9
|
} from '@directus/sdk';
|
|
10
10
|
import { AuthLocalStorage } from '../storage/local-storage';
|
|
11
|
-
import type { DirectusConfig } from '../types/adapter.types';
|
|
11
|
+
import type { DirectusAdapterClient, DirectusConfig, SchemaShape } from '../types/adapter.types';
|
|
12
12
|
|
|
13
13
|
const authConfigDefaults = {
|
|
14
14
|
autoRefresh: false,
|
|
@@ -19,13 +19,13 @@ const authConfigDefaults = {
|
|
|
19
19
|
/**
|
|
20
20
|
* Creates a directus client based on the application configuration
|
|
21
21
|
*/
|
|
22
|
-
export const createDirectusClientFactory = <T extends
|
|
22
|
+
export const createDirectusClientFactory = <T extends SchemaShape>(
|
|
23
23
|
{
|
|
24
24
|
baseUrl,
|
|
25
25
|
configuration: { protocol = 'https', host, auth, staticToken: token, graphql: gql, rest: rs }
|
|
26
26
|
}: DirectusConfig,
|
|
27
27
|
options?: ClientOptions
|
|
28
|
-
) => {
|
|
28
|
+
): DirectusAdapterClient<T> => {
|
|
29
29
|
let url = baseUrl;
|
|
30
30
|
|
|
31
31
|
if (host) {
|
|
@@ -42,7 +42,7 @@ export const createDirectusClientFactory = <T extends object>(
|
|
|
42
42
|
rs = { credentials: 'include' };
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
let client = createDirectus<T>(url, options).with(rest(rs))
|
|
45
|
+
let client = createDirectus<T>(url, options).with(rest(rs)) as DirectusAdapterClient<T>;
|
|
46
46
|
|
|
47
47
|
if (auth) {
|
|
48
48
|
const { config: c, mode } = auth;
|
|
@@ -54,15 +54,15 @@ export const createDirectusClientFactory = <T extends object>(
|
|
|
54
54
|
authConfig.storage = new AuthLocalStorage();
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
client = client.with(authentication(mode, authConfig))
|
|
57
|
+
client = client.with(authentication(mode, authConfig)) as DirectusAdapterClient<T>;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
if (token) {
|
|
61
|
-
client = client.with(staticToken(token))
|
|
61
|
+
client = client.with(staticToken(token)) as DirectusAdapterClient<T>;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
if (gql) {
|
|
65
|
-
client = client.with(graphql(gql))
|
|
65
|
+
client = client.with(graphql(gql)) as DirectusAdapterClient<T>;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
return client;
|
package/src/services/auth.ts
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
|
-
import type { AuthenticationClient, DirectusClient, RestClient } from '@directus/sdk';
|
|
2
1
|
import { readFiles, readMe, updateMe } from '@directus/sdk';
|
|
3
2
|
import { ServiceAbstract } from '@tanglemedia/svelte-starter-core';
|
|
3
|
+
import type { DirectusAdapterClient, SchemaShape } from '../types';
|
|
4
|
+
|
|
5
|
+
type AuthenticatedDirectusClient = DirectusAdapterClient<SchemaShape> &
|
|
6
|
+
Required<Pick<DirectusAdapterClient<SchemaShape>, 'refresh' | 'getToken' | 'login' | 'logout'>>;
|
|
4
7
|
|
|
5
8
|
class DirectusAuthServices extends ServiceAbstract {
|
|
6
9
|
public getDirectus() {
|
|
7
|
-
return this.getAdapterClient<
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
return this.getAdapterClient<DirectusAdapterClient<SchemaShape>>();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
private async getAuthenticatedDirectus(): Promise<AuthenticatedDirectusClient> {
|
|
14
|
+
const directus = await this.getDirectus();
|
|
15
|
+
|
|
16
|
+
if (!directus.refresh || !directus.getToken || !directus.login || !directus.logout) {
|
|
17
|
+
throw new Error('Directus client is missing authentication methods');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return directus as AuthenticatedDirectusClient;
|
|
10
21
|
}
|
|
11
22
|
|
|
12
23
|
// public getConfig<T extends object>(): DirectusClient<T> & RestClient<object> {
|
|
@@ -15,7 +26,7 @@ class DirectusAuthServices extends ServiceAbstract {
|
|
|
15
26
|
|
|
16
27
|
public async refresh() {
|
|
17
28
|
try {
|
|
18
|
-
const response = await (await this.
|
|
29
|
+
const response = await (await this.getAuthenticatedDirectus()).refresh();
|
|
19
30
|
return response;
|
|
20
31
|
} catch (error) {
|
|
21
32
|
console.error(`Error on directus sdk refresh() :`, error);
|
|
@@ -25,7 +36,7 @@ class DirectusAuthServices extends ServiceAbstract {
|
|
|
25
36
|
|
|
26
37
|
public async getToken() {
|
|
27
38
|
try {
|
|
28
|
-
const response = await (await this.
|
|
39
|
+
const response = await (await this.getAuthenticatedDirectus()).getToken();
|
|
29
40
|
return response;
|
|
30
41
|
} catch (error) {
|
|
31
42
|
console.error(`Error on directus sdk getToken() :`, error);
|
|
@@ -35,7 +46,7 @@ class DirectusAuthServices extends ServiceAbstract {
|
|
|
35
46
|
|
|
36
47
|
public async login(email: string, password: string) {
|
|
37
48
|
try {
|
|
38
|
-
const response = await (await this.
|
|
49
|
+
const response = await (await this.getAuthenticatedDirectus()).login({ email, password });
|
|
39
50
|
return response;
|
|
40
51
|
} catch (error) {
|
|
41
52
|
console.error(`Error on directus sdk login() :`, error);
|
|
@@ -45,7 +56,7 @@ class DirectusAuthServices extends ServiceAbstract {
|
|
|
45
56
|
|
|
46
57
|
public async logout() {
|
|
47
58
|
try {
|
|
48
|
-
const response = await (await this.
|
|
59
|
+
const response = await (await this.getAuthenticatedDirectus()).logout();
|
|
49
60
|
return response;
|
|
50
61
|
} catch (error) {
|
|
51
62
|
console.error(`Error on directus sdk logout() :`, error);
|
|
@@ -56,9 +67,8 @@ class DirectusAuthServices extends ServiceAbstract {
|
|
|
56
67
|
public async getCurrentUser<T>(fields: string[] | null = null): Promise<T> {
|
|
57
68
|
try {
|
|
58
69
|
if (!(await this.getToken())) await this.refresh();
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
).request<T>(readMe({ fields: fields ? fields : ['*', 'roles.*'] }));
|
|
70
|
+
const queryFields = (fields ?? ['*', 'roles.*']) as never;
|
|
71
|
+
const response = await (await this.getDirectus()).request<T>(readMe({ fields: queryFields }));
|
|
62
72
|
return response;
|
|
63
73
|
} catch (error) {
|
|
64
74
|
console.error(`Error on directus sdk readMe() :`, error);
|
package/src/services/register.ts
CHANGED
|
@@ -17,7 +17,7 @@ class DirectusStaticServices extends ServiceAdapterAbstract {
|
|
|
17
17
|
try {
|
|
18
18
|
await (await this.getDirectus()).request(createUser(data));
|
|
19
19
|
|
|
20
|
-
return { message: 'You have
|
|
20
|
+
return { message: 'You have registered your account', type: 'success' };
|
|
21
21
|
} catch (error) {
|
|
22
22
|
console.error(`Error creating a user:`, error);
|
|
23
23
|
throw error;
|
|
@@ -1,15 +1,25 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
AuthenticationClient,
|
|
3
|
+
AuthenticationMode,
|
|
4
|
+
DirectusClient,
|
|
5
|
+
GraphqlClient,
|
|
6
|
+
GraphqlConfig,
|
|
7
|
+
RestClient,
|
|
8
|
+
StaticTokenClient
|
|
9
|
+
} from '@directus/sdk';
|
|
2
10
|
import type { BaseApiAdapterConfig } from '@tanglemedia/svelte-starter-core';
|
|
3
11
|
|
|
4
|
-
export {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
export type {
|
|
13
|
+
AllCollections,
|
|
14
|
+
RegularCollections,
|
|
15
|
+
DirectusClient,
|
|
16
|
+
RestClient,
|
|
17
|
+
StaticTokenClient,
|
|
18
|
+
AuthenticationClient
|
|
11
19
|
} from '@directus/sdk';
|
|
12
20
|
|
|
21
|
+
export type * from '@directus/types';
|
|
22
|
+
|
|
13
23
|
export type DirectusApiAuthOptions = {
|
|
14
24
|
mode: AuthenticationMode;
|
|
15
25
|
config?: Partial<{
|
|
@@ -40,4 +50,11 @@ export type DirectusApiAdapterOptions = {
|
|
|
40
50
|
|
|
41
51
|
export type DirectusConfig = BaseApiAdapterConfig<DirectusApiAdapterOptions>;
|
|
42
52
|
|
|
43
|
-
export type SchemaShape = Record<string, object>;
|
|
53
|
+
export type SchemaShape = Record<string, object | object[]>;
|
|
54
|
+
|
|
55
|
+
export type DirectusAdapterClient<Schema extends SchemaShape = SchemaShape> =
|
|
56
|
+
DirectusClient<Schema> &
|
|
57
|
+
RestClient<Schema> &
|
|
58
|
+
Partial<AuthenticationClient<Schema>> &
|
|
59
|
+
Partial<StaticTokenClient<Schema>> &
|
|
60
|
+
Partial<GraphqlClient<Schema>>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
export type DirectusErrorCode =
|
|
2
|
+
| 'FAILED_VALIDATION'
|
|
3
|
+
| 'FORBIDDEN'
|
|
4
|
+
| 'INVALID_TOKEN'
|
|
5
|
+
| 'TOKEN_EXPIRED'
|
|
6
|
+
| 'INVALID_CREDENTIALS'
|
|
7
|
+
| 'INVALID_IP'
|
|
8
|
+
| 'INVALID_OTP'
|
|
9
|
+
| 'INVALID_PAYLOAD'
|
|
10
|
+
| 'INVALID_QUERY'
|
|
11
|
+
| 'UNSUPPORTED_MEDIA_TYPE'
|
|
12
|
+
| 'REQUESTS_EXCEEDED'
|
|
13
|
+
| 'ROUTE_NOT_FOUND'
|
|
14
|
+
| 'SERVICE_UNAVAILABLE'
|
|
15
|
+
| 'UNPROCESSABLE_CONTENT';
|
|
16
|
+
|
|
17
|
+
type DirectusErrorExtensions = { code: string };
|
|
18
|
+
|
|
19
|
+
type DirectusErrorEntry = {
|
|
20
|
+
message: string;
|
|
21
|
+
extensions: DirectusErrorExtensions;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type DirectusErrorShape = {
|
|
25
|
+
errors: DirectusErrorEntry[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function parseDirectusErrorCandidate(err: unknown): unknown {
|
|
29
|
+
if (typeof err !== 'object' || err === null) {
|
|
30
|
+
return err;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (isDirectusErrorShape(err)) {
|
|
34
|
+
return err;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (!(err instanceof Error)) {
|
|
38
|
+
return err;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
return JSON.parse(err.message) as unknown;
|
|
43
|
+
} catch {
|
|
44
|
+
return err;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function isDirectusErrorShape(err: unknown): err is DirectusErrorShape {
|
|
49
|
+
if (typeof err !== 'object' || err === null) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
const candidate = err as Record<string, unknown>;
|
|
53
|
+
if (!Array.isArray(candidate['errors']) || candidate['errors'].length === 0) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
const first = candidate['errors'][0] as Record<string, unknown>;
|
|
57
|
+
return (
|
|
58
|
+
typeof first['extensions'] === 'object' &&
|
|
59
|
+
first['extensions'] !== null &&
|
|
60
|
+
'code' in (first['extensions'] as object)
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Returns the Directus error code from the first error in a Directus API error response,
|
|
66
|
+
* or null if the value is not a Directus error shape.
|
|
67
|
+
*/
|
|
68
|
+
export function extractDirectusErrorCode(err: unknown): DirectusErrorCode | null {
|
|
69
|
+
const candidate = parseDirectusErrorCandidate(err);
|
|
70
|
+
if (!isDirectusErrorShape(candidate)) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
const code = (candidate.errors[0].extensions as DirectusErrorExtensions).code;
|
|
74
|
+
return (code as DirectusErrorCode) ?? null;
|
|
75
|
+
}
|