@webiny/api-aco 0.0.0-unstable.aad28a72ae
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 +18 -0
- package/context/folders.d.ts +2 -0
- package/context/folders.js +261 -0
- package/context/folders.js.map +1 -0
- package/context/index.d.ts +2 -0
- package/context/index.js +22 -0
- package/context/index.js.map +1 -0
- package/context/links.d.ts +2 -0
- package/context/links.js +270 -0
- package/context/links.js.map +1 -0
- package/graphql/base.gql.d.ts +3 -0
- package/graphql/base.gql.js +63 -0
- package/graphql/base.gql.js.map +1 -0
- package/graphql/folders.gql.d.ts +3 -0
- package/graphql/folders.gql.js +126 -0
- package/graphql/folders.gql.js.map +1 -0
- package/graphql/index.d.ts +1 -0
- package/graphql/index.js +15 -0
- package/graphql/index.js.map +1 -0
- package/graphql/links.gql.d.ts +3 -0
- package/graphql/links.gql.js +123 -0
- package/graphql/links.gql.js.map +1 -0
- package/index.d.ts +9 -0
- package/index.js +71 -0
- package/index.js.map +1 -0
- package/package.json +55 -0
- package/subscriptions/afterFolderDelete.d.ts +3 -0
- package/subscriptions/afterFolderDelete.js +62 -0
- package/subscriptions/afterFolderDelete.js.map +1 -0
- package/subscriptions/index.d.ts +3 -0
- package/subscriptions/index.js +14 -0
- package/subscriptions/index.js.map +1 -0
- package/types.d.ts +219 -0
- package/types.js +5 -0
- package/types.js.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Webiny
|
|
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
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# @webiny/api-aco
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@webiny/api-aco)
|
|
4
|
+
[](https://www.npmjs.com/package/@webiny/api-aco)
|
|
5
|
+
[](https://github.com/prettier/prettier)
|
|
6
|
+
[](http://makeapullrequest.com)
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
npm install --save @webiny/api-aco
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Or if you prefer yarn:
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
yarn add @webiny/api-aco
|
|
18
|
+
```
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.createFoldersContext = void 0;
|
|
9
|
+
|
|
10
|
+
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
|
+
|
|
12
|
+
var _mdbid = _interopRequireDefault(require("mdbid"));
|
|
13
|
+
|
|
14
|
+
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
15
|
+
|
|
16
|
+
var _handlerGraphql = require("@webiny/handler-graphql");
|
|
17
|
+
|
|
18
|
+
var _pubsub = require("@webiny/pubsub");
|
|
19
|
+
|
|
20
|
+
var _joi = _interopRequireDefault(require("joi"));
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Package mdbid does not have types.
|
|
24
|
+
*/
|
|
25
|
+
// @ts-ignore
|
|
26
|
+
const requiredString = _joi.default.string().required();
|
|
27
|
+
|
|
28
|
+
const createSchema = _joi.default.object({
|
|
29
|
+
name: requiredString.min(3),
|
|
30
|
+
slug: requiredString.min(3),
|
|
31
|
+
type: requiredString,
|
|
32
|
+
tenant: requiredString,
|
|
33
|
+
locale: requiredString
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const updateSchema = _joi.default.object({
|
|
37
|
+
name: requiredString.min(3),
|
|
38
|
+
slug: requiredString.min(3)
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const createFoldersContext = async ({
|
|
42
|
+
getTenantId,
|
|
43
|
+
getLocaleCode,
|
|
44
|
+
getIdentity,
|
|
45
|
+
storageOperations
|
|
46
|
+
}) => {
|
|
47
|
+
// create
|
|
48
|
+
const onFolderBeforeCreate = (0, _pubsub.createTopic)("folders.onFolderBeforeCreate");
|
|
49
|
+
const onFolderAfterCreate = (0, _pubsub.createTopic)("folders.onFolderAfterCreate"); // update
|
|
50
|
+
|
|
51
|
+
const onFolderBeforeUpdate = (0, _pubsub.createTopic)("folders.onFolderBeforeUpdate");
|
|
52
|
+
const onFolderAfterUpdate = (0, _pubsub.createTopic)("folders.onFolderAfterUpdate"); // delete
|
|
53
|
+
|
|
54
|
+
const onFolderBeforeDelete = (0, _pubsub.createTopic)("folders.onFolderBeforeDelete");
|
|
55
|
+
const onFolderAfterDelete = (0, _pubsub.createTopic)("folders.onFolderAfterDelete");
|
|
56
|
+
return {
|
|
57
|
+
onFolderBeforeCreate,
|
|
58
|
+
onFolderAfterCreate,
|
|
59
|
+
onFolderBeforeUpdate,
|
|
60
|
+
onFolderAfterUpdate,
|
|
61
|
+
onFolderBeforeDelete,
|
|
62
|
+
onFolderAfterDelete,
|
|
63
|
+
|
|
64
|
+
async getFolder({
|
|
65
|
+
id
|
|
66
|
+
}) {
|
|
67
|
+
const tenant = getTenantId();
|
|
68
|
+
const locale = getLocaleCode();
|
|
69
|
+
let folder;
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
folder = await storageOperations.getFolder({
|
|
73
|
+
tenant,
|
|
74
|
+
locale,
|
|
75
|
+
id
|
|
76
|
+
});
|
|
77
|
+
} catch (error) {
|
|
78
|
+
throw _error.default.from(error, {
|
|
79
|
+
message: "Could not get folder.",
|
|
80
|
+
code: "GET_FOLDER_ERROR",
|
|
81
|
+
data: {
|
|
82
|
+
id
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (!folder) {
|
|
88
|
+
throw new _handlerGraphql.NotFoundError(`Unable to find folder with id: ${id}`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return folder;
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
async listFolders({
|
|
95
|
+
where
|
|
96
|
+
}) {
|
|
97
|
+
const tenant = getTenantId();
|
|
98
|
+
const locale = getLocaleCode();
|
|
99
|
+
|
|
100
|
+
try {
|
|
101
|
+
return await storageOperations.listFolders({
|
|
102
|
+
where: (0, _objectSpread2.default)({
|
|
103
|
+
tenant,
|
|
104
|
+
locale
|
|
105
|
+
}, where),
|
|
106
|
+
sort: ["createdOn_ASC"]
|
|
107
|
+
});
|
|
108
|
+
} catch (error) {
|
|
109
|
+
throw _error.default.from(error, {
|
|
110
|
+
message: "Could not list folders.",
|
|
111
|
+
code: "LIST_FOLDERS_ERROR",
|
|
112
|
+
data: (0, _objectSpread2.default)({}, where)
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
async createFolder(input) {
|
|
118
|
+
await createSchema.validate(input);
|
|
119
|
+
const tenant = getTenantId();
|
|
120
|
+
const locale = getLocaleCode();
|
|
121
|
+
const {
|
|
122
|
+
type,
|
|
123
|
+
slug,
|
|
124
|
+
parentId
|
|
125
|
+
} = input;
|
|
126
|
+
const existing = await storageOperations.getFolder({
|
|
127
|
+
tenant,
|
|
128
|
+
locale,
|
|
129
|
+
type,
|
|
130
|
+
slug,
|
|
131
|
+
parentId
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
if (existing) {
|
|
135
|
+
throw new _error.default(`Folder with slug "${slug}" already exists at this level.`, "FOLDER_EXISTS");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const identity = getIdentity();
|
|
139
|
+
const folder = (0, _objectSpread2.default)((0, _objectSpread2.default)({
|
|
140
|
+
id: (0, _mdbid.default)(),
|
|
141
|
+
tenant,
|
|
142
|
+
locale
|
|
143
|
+
}, input), {}, {
|
|
144
|
+
webinyVersion: process.env.WEBINY_VERSION,
|
|
145
|
+
createdOn: new Date().toISOString(),
|
|
146
|
+
createdBy: {
|
|
147
|
+
id: identity.id,
|
|
148
|
+
displayName: identity.displayName,
|
|
149
|
+
type: identity.type
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
await onFolderBeforeCreate.publish({
|
|
155
|
+
folder
|
|
156
|
+
});
|
|
157
|
+
const result = await storageOperations.createFolder({
|
|
158
|
+
folder
|
|
159
|
+
});
|
|
160
|
+
await onFolderAfterCreate.publish({
|
|
161
|
+
folder: result
|
|
162
|
+
});
|
|
163
|
+
return result;
|
|
164
|
+
} catch (error) {
|
|
165
|
+
throw _error.default.from(error, {
|
|
166
|
+
message: "Could not create folder.",
|
|
167
|
+
code: "CREATE_FOLDER_ERROR",
|
|
168
|
+
data: (0, _objectSpread2.default)({}, input)
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
async updateFolder(id, input) {
|
|
174
|
+
await updateSchema.validate(input);
|
|
175
|
+
const tenant = getTenantId();
|
|
176
|
+
const locale = getLocaleCode();
|
|
177
|
+
const {
|
|
178
|
+
slug,
|
|
179
|
+
parentId
|
|
180
|
+
} = input;
|
|
181
|
+
const original = await storageOperations.getFolder({
|
|
182
|
+
tenant,
|
|
183
|
+
locale,
|
|
184
|
+
id
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
if (!original) {
|
|
188
|
+
throw new _handlerGraphql.NotFoundError(`Folder "${id}" was not found!`);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const existing = await storageOperations.getFolder({
|
|
192
|
+
tenant,
|
|
193
|
+
locale,
|
|
194
|
+
type: original.type,
|
|
195
|
+
slug: slug || original.slug,
|
|
196
|
+
parentId: parentId !== undefined ? parentId : original.parentId // parentId can be `null`
|
|
197
|
+
|
|
198
|
+
}); // Check if another folder exists already inside the target
|
|
199
|
+
|
|
200
|
+
if (existing && (existing === null || existing === void 0 ? void 0 : existing.id) !== id) {
|
|
201
|
+
throw new _error.default(`Folder with slug "${slug}" already exists at this level.`, "FOLDER_EXISTS");
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const folder = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, original), input);
|
|
205
|
+
|
|
206
|
+
try {
|
|
207
|
+
await onFolderBeforeUpdate.publish({
|
|
208
|
+
folder,
|
|
209
|
+
original
|
|
210
|
+
});
|
|
211
|
+
const result = await storageOperations.updateFolder({
|
|
212
|
+
original,
|
|
213
|
+
folder
|
|
214
|
+
});
|
|
215
|
+
await onFolderAfterUpdate.publish({
|
|
216
|
+
folder: result,
|
|
217
|
+
original
|
|
218
|
+
});
|
|
219
|
+
return result;
|
|
220
|
+
} catch (error) {
|
|
221
|
+
throw new _error.default(error.message || "Could not update folder.", error.code || "UPDATE_FOLDER_ERROR", {
|
|
222
|
+
folder
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
|
|
227
|
+
async deleteFolder(id) {
|
|
228
|
+
const tenant = getTenantId();
|
|
229
|
+
const locale = getLocaleCode();
|
|
230
|
+
const folder = await storageOperations.getFolder({
|
|
231
|
+
tenant,
|
|
232
|
+
locale,
|
|
233
|
+
id
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
if (!folder) {
|
|
237
|
+
throw new _handlerGraphql.NotFoundError(`Folder "${id}" was not found!`);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
try {
|
|
241
|
+
await onFolderBeforeDelete.publish({
|
|
242
|
+
folder
|
|
243
|
+
});
|
|
244
|
+
const result = storageOperations.deleteFolder({
|
|
245
|
+
folder
|
|
246
|
+
});
|
|
247
|
+
await onFolderAfterDelete.publish({
|
|
248
|
+
folder
|
|
249
|
+
});
|
|
250
|
+
return result;
|
|
251
|
+
} catch (error) {
|
|
252
|
+
throw new _error.default(error.message || "Could not delete folder.", error.code || "DELETE_FOLDER_ERROR", {
|
|
253
|
+
folder
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
};
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
exports.createFoldersContext = createFoldersContext;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["requiredString","joi","string","required","createSchema","object","name","min","slug","type","tenant","locale","updateSchema","createFoldersContext","getTenantId","getLocaleCode","getIdentity","storageOperations","onFolderBeforeCreate","createTopic","onFolderAfterCreate","onFolderBeforeUpdate","onFolderAfterUpdate","onFolderBeforeDelete","onFolderAfterDelete","getFolder","id","folder","error","WebinyError","from","message","code","data","NotFoundError","listFolders","where","sort","createFolder","input","validate","parentId","existing","identity","mdbid","webinyVersion","process","env","WEBINY_VERSION","createdOn","Date","toISOString","createdBy","displayName","publish","result","updateFolder","original","undefined","deleteFolder"],"sources":["folders.ts"],"sourcesContent":["/**\n * Package mdbid does not have types.\n */\n// @ts-ignore\nimport mdbid from \"mdbid\";\nimport WebinyError from \"@webiny/error\";\nimport { NotFoundError } from \"@webiny/handler-graphql\";\nimport { createTopic } from \"@webiny/pubsub\";\nimport joi from \"joi\";\n\nimport {\n Folder,\n FolderInput,\n FoldersConfig,\n GetFolderParams,\n IFolders,\n ListFoldersParams,\n OnFolderAfterCreateTopicParams,\n OnFolderAfterDeleteTopicParams,\n OnFolderAfterUpdateTopicParams,\n OnFolderBeforeCreateTopicParams,\n OnFolderBeforeDeleteTopicParams,\n OnFolderBeforeUpdateTopicParams\n} from \"~/types\";\n\nconst requiredString = joi.string().required();\n\nconst createSchema = joi.object({\n name: requiredString.min(3),\n slug: requiredString.min(3),\n type: requiredString,\n tenant: requiredString,\n locale: requiredString\n});\n\nconst updateSchema = joi.object({\n name: requiredString.min(3),\n slug: requiredString.min(3)\n});\n\nexport const createFoldersContext = async ({\n getTenantId,\n getLocaleCode,\n getIdentity,\n storageOperations\n}: FoldersConfig): Promise<IFolders> => {\n // create\n const onFolderBeforeCreate = createTopic<OnFolderBeforeCreateTopicParams>(\n \"folders.onFolderBeforeCreate\"\n );\n const onFolderAfterCreate = createTopic<OnFolderAfterCreateTopicParams>(\n \"folders.onFolderAfterCreate\"\n );\n // update\n const onFolderBeforeUpdate = createTopic<OnFolderBeforeUpdateTopicParams>(\n \"folders.onFolderBeforeUpdate\"\n );\n const onFolderAfterUpdate = createTopic<OnFolderAfterUpdateTopicParams>(\n \"folders.onFolderAfterUpdate\"\n );\n // delete\n const onFolderBeforeDelete = createTopic<OnFolderBeforeDeleteTopicParams>(\n \"folders.onFolderBeforeDelete\"\n );\n const onFolderAfterDelete = createTopic<OnFolderAfterDeleteTopicParams>(\n \"folders.onFolderAfterDelete\"\n );\n\n return {\n onFolderBeforeCreate,\n onFolderAfterCreate,\n onFolderBeforeUpdate,\n onFolderAfterUpdate,\n onFolderBeforeDelete,\n onFolderAfterDelete,\n async getFolder({ id }: GetFolderParams): Promise<Folder> {\n const tenant = getTenantId();\n const locale = getLocaleCode();\n\n let folder: Folder | undefined;\n\n try {\n folder = await storageOperations.getFolder({ tenant, locale, id });\n } catch (error) {\n throw WebinyError.from(error, {\n message: \"Could not get folder.\",\n code: \"GET_FOLDER_ERROR\",\n data: { id }\n });\n }\n if (!folder) {\n throw new NotFoundError(`Unable to find folder with id: ${id}`);\n }\n return folder;\n },\n\n async listFolders({ where }: ListFoldersParams): Promise<Folder[]> {\n const tenant = getTenantId();\n const locale = getLocaleCode();\n\n try {\n return await storageOperations.listFolders({\n where: { tenant, locale, ...where },\n sort: [\"createdOn_ASC\"]\n });\n } catch (error) {\n throw WebinyError.from(error, {\n message: \"Could not list folders.\",\n code: \"LIST_FOLDERS_ERROR\",\n data: { ...where }\n });\n }\n },\n\n async createFolder(input: FolderInput): Promise<Folder> {\n await createSchema.validate(input);\n\n const tenant = getTenantId();\n const locale = getLocaleCode();\n const { type, slug, parentId } = input;\n\n const existing = await storageOperations.getFolder({\n tenant,\n locale,\n type,\n slug,\n parentId\n });\n\n if (existing) {\n throw new WebinyError(\n `Folder with slug \"${slug}\" already exists at this level.`,\n \"FOLDER_EXISTS\"\n );\n }\n\n const identity = getIdentity();\n\n const folder: Folder = {\n id: mdbid(),\n tenant,\n locale,\n ...input,\n webinyVersion: process.env.WEBINY_VERSION as string,\n createdOn: new Date().toISOString(),\n createdBy: {\n id: identity.id,\n displayName: identity.displayName,\n type: identity.type\n }\n };\n\n try {\n await onFolderBeforeCreate.publish({\n folder\n });\n const result = await storageOperations.createFolder({ folder });\n await onFolderAfterCreate.publish({\n folder: result\n });\n return result;\n } catch (error) {\n throw WebinyError.from(error, {\n message: \"Could not create folder.\",\n code: \"CREATE_FOLDER_ERROR\",\n data: { ...input }\n });\n }\n },\n\n async updateFolder(id: string, input: Record<string, any>): Promise<Folder> {\n await updateSchema.validate(input);\n\n const tenant = getTenantId();\n const locale = getLocaleCode();\n const { slug, parentId } = input;\n\n const original = await storageOperations.getFolder({ tenant, locale, id });\n\n if (!original) {\n throw new NotFoundError(`Folder \"${id}\" was not found!`);\n }\n\n const existing = await storageOperations.getFolder({\n tenant,\n locale,\n type: original.type,\n slug: slug || original.slug,\n parentId: parentId !== undefined ? parentId : original.parentId // parentId can be `null`\n });\n\n // Check if another folder exists already inside the target\n if (existing && existing?.id !== id) {\n throw new WebinyError(\n `Folder with slug \"${slug}\" already exists at this level.`,\n \"FOLDER_EXISTS\"\n );\n }\n\n const folder: Folder = {\n ...original,\n ...input\n };\n try {\n await onFolderBeforeUpdate.publish({\n folder,\n original\n });\n const result = await storageOperations.updateFolder({ original, folder });\n await onFolderAfterUpdate.publish({\n folder: result,\n original\n });\n return result;\n } catch (error) {\n throw new WebinyError(\n error.message || \"Could not update folder.\",\n error.code || \"UPDATE_FOLDER_ERROR\",\n {\n folder\n }\n );\n }\n },\n\n async deleteFolder(id: string): Promise<void> {\n const tenant = getTenantId();\n const locale = getLocaleCode();\n\n const folder = await storageOperations.getFolder({ tenant, locale, id });\n\n if (!folder) {\n throw new NotFoundError(`Folder \"${id}\" was not found!`);\n }\n\n try {\n await onFolderBeforeDelete.publish({\n folder\n });\n const result = storageOperations.deleteFolder({ folder });\n await onFolderAfterDelete.publish({\n folder\n });\n return result;\n } catch (error) {\n throw new WebinyError(\n error.message || \"Could not delete folder.\",\n error.code || \"DELETE_FOLDER_ERROR\",\n {\n folder\n }\n );\n }\n }\n };\n};\n"],"mappings":";;;;;;;;;;;AAIA;;AACA;;AACA;;AACA;;AACA;;AARA;AACA;AACA;AACA;AAsBA,MAAMA,cAAc,GAAGC,YAAA,CAAIC,MAAJ,GAAaC,QAAb,EAAvB;;AAEA,MAAMC,YAAY,GAAGH,YAAA,CAAII,MAAJ,CAAW;EAC5BC,IAAI,EAAEN,cAAc,CAACO,GAAf,CAAmB,CAAnB,CADsB;EA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3B,KAAP,EAAc;QACZ,MAAM,IAAIC,cAAJ,CACFD,KAAK,CAACG,OAAN,IAAiB,0BADf,EAEFH,KAAK,CAACI,IAAN,IAAc,qBAFZ,EAGF;UACIL;QADJ,CAHE,CAAN;MAOH;IACJ,CA3JE;;IA6JH,MAAMgC,YAAN,CAAmBjC,EAAnB,EAA8C;MAC1C,MAAMhB,MAAM,GAAGI,WAAW,EAA1B;MACA,MAAMH,MAAM,GAAGI,aAAa,EAA5B;MAEA,MAAMY,MAAM,GAAG,MAAMV,iBAAiB,CAACQ,SAAlB,CAA4B;QAAEf,MAAF;QAAUC,MAAV;QAAkBe;MAAlB,CAA5B,CAArB;;MAEA,IAAI,CAACC,MAAL,EAAa;QACT,MAAM,IAAIO,6BAAJ,CAAmB,WAAUR,EAAG,kBAAhC,CAAN;MACH;;MAED,IAAI;QACA,MAAMH,oBAAoB,CAAC+B,OAArB,CAA6B;UAC/B3B;QAD+B,CAA7B,CAAN;QAGA,MAAM4B,MAAM,GAAGtC,iBAAiB,CAAC0C,YAAlB,CAA+B;UAAEhC;QAAF,CAA/B,CAAf;QACA,MAAMH,mBAAmB,CAAC8B,OAApB,CAA4B;UAC9B3B;QAD8B,CAA5B,CAAN;QAGA,OAAO4B,MAAP;MACH,CATD,CASE,OAAO3B,KAAP,EAAc;QACZ,MAAM,IAAIC,cAAJ,CACFD,KAAK,CAACG,OAAN,IAAiB,0BADf,EAEFH,KAAK,CAACI,IAAN,IAAc,qBAFZ,EAGF;UACIL;QADJ,CAHE,CAAN;MAOH;IACJ;;EAzLE,CAAP;AA2LH,CAvNM"}
|
package/context/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.createContext = void 0;
|
|
9
|
+
|
|
10
|
+
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
|
+
|
|
12
|
+
var _folders = require("./folders");
|
|
13
|
+
|
|
14
|
+
var _links = require("./links");
|
|
15
|
+
|
|
16
|
+
const createContext = async config => {
|
|
17
|
+
const linksContext = await (0, _links.createLinksContext)(config);
|
|
18
|
+
const folderContext = await (0, _folders.createFoldersContext)(config);
|
|
19
|
+
return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, linksContext), folderContext);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
exports.createContext = createContext;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["createContext","config","linksContext","createLinksContext","folderContext","createFoldersContext"],"sources":["index.ts"],"sourcesContent":["import { createFoldersContext } from \"./folders\";\nimport { createLinksContext } from \"./links\";\n\nimport { Folders, FoldersConfig } from \"~/types\";\n\nexport const createContext = async (config: FoldersConfig): Promise<Folders> => {\n const linksContext = await createLinksContext(config);\n const folderContext = await createFoldersContext(config);\n\n return {\n ...linksContext,\n ...folderContext\n };\n};\n"],"mappings":";;;;;;;;;;;AAAA;;AACA;;AAIO,MAAMA,aAAa,GAAG,MAAOC,MAAP,IAAmD;EAC5E,MAAMC,YAAY,GAAG,MAAM,IAAAC,yBAAA,EAAmBF,MAAnB,CAA3B;EACA,MAAMG,aAAa,GAAG,MAAM,IAAAC,6BAAA,EAAqBJ,MAArB,CAA5B;EAEA,mEACOC,YADP,GAEOE,aAFP;AAIH,CARM"}
|
package/context/links.js
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.createLinksContext = void 0;
|
|
9
|
+
|
|
10
|
+
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
|
+
|
|
12
|
+
var _mdbid = _interopRequireDefault(require("mdbid"));
|
|
13
|
+
|
|
14
|
+
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
15
|
+
|
|
16
|
+
var _handlerGraphql = require("@webiny/handler-graphql");
|
|
17
|
+
|
|
18
|
+
var _pubsub = require("@webiny/pubsub");
|
|
19
|
+
|
|
20
|
+
var _joi = _interopRequireDefault(require("joi"));
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Package mdbid does not have types.
|
|
24
|
+
*/
|
|
25
|
+
// @ts-ignore
|
|
26
|
+
const requiredString = _joi.default.string().required();
|
|
27
|
+
|
|
28
|
+
const createSchema = _joi.default.object({
|
|
29
|
+
id: requiredString,
|
|
30
|
+
folderId: requiredString,
|
|
31
|
+
tenant: requiredString,
|
|
32
|
+
locale: requiredString
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const updateSchema = _joi.default.object({
|
|
36
|
+
folderId: requiredString
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const createLinksContext = async ({
|
|
40
|
+
getTenantId,
|
|
41
|
+
getLocaleCode,
|
|
42
|
+
getIdentity,
|
|
43
|
+
storageOperations
|
|
44
|
+
}) => {
|
|
45
|
+
// create
|
|
46
|
+
const onLinkBeforeCreate = (0, _pubsub.createTopic)("folders.onLinkBeforeCreate");
|
|
47
|
+
const onLinkAfterCreate = (0, _pubsub.createTopic)("folders.onLinkAfterCreate"); // update
|
|
48
|
+
|
|
49
|
+
const onLinkBeforeUpdate = (0, _pubsub.createTopic)("folders.onLinkBeforeUpdate");
|
|
50
|
+
const onLinkAfterUpdate = (0, _pubsub.createTopic)("folders.onLinkAfterUpdate"); // delete
|
|
51
|
+
|
|
52
|
+
const onLinkBeforeDelete = (0, _pubsub.createTopic)("folders.onLinkBeforeDelete");
|
|
53
|
+
const onLinkAfterDelete = (0, _pubsub.createTopic)("folders.onLinkAfterDelete"); // delete batch
|
|
54
|
+
|
|
55
|
+
const onLinkBeforeDeleteBatch = (0, _pubsub.createTopic)("folders.onLinkBeforeDeleteBatch");
|
|
56
|
+
const onLinkAfterDeleteBatch = (0, _pubsub.createTopic)("folders.onLinkAfterDeleteBatch");
|
|
57
|
+
return {
|
|
58
|
+
onLinkBeforeCreate,
|
|
59
|
+
onLinkAfterCreate,
|
|
60
|
+
onLinkBeforeUpdate,
|
|
61
|
+
onLinkAfterUpdate,
|
|
62
|
+
onLinkBeforeDelete,
|
|
63
|
+
onLinkAfterDelete,
|
|
64
|
+
onLinkBeforeDeleteBatch,
|
|
65
|
+
onLinkAfterDeleteBatch,
|
|
66
|
+
|
|
67
|
+
async getLink(id) {
|
|
68
|
+
const tenant = getTenantId();
|
|
69
|
+
const locale = getLocaleCode();
|
|
70
|
+
let link;
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
link = await storageOperations.getLink({
|
|
74
|
+
tenant,
|
|
75
|
+
locale,
|
|
76
|
+
id
|
|
77
|
+
});
|
|
78
|
+
} catch (error) {
|
|
79
|
+
throw _error.default.from(error, {
|
|
80
|
+
message: "Could not get link.",
|
|
81
|
+
code: "GET_LINK_ERROR",
|
|
82
|
+
data: {
|
|
83
|
+
id
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (!link) {
|
|
89
|
+
throw new _handlerGraphql.NotFoundError(`Unable to find link with id: ${id}`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return link;
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
async listLinks({
|
|
96
|
+
where,
|
|
97
|
+
limit,
|
|
98
|
+
after
|
|
99
|
+
}) {
|
|
100
|
+
const tenant = getTenantId();
|
|
101
|
+
const locale = getLocaleCode();
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
return await storageOperations.listLinks({
|
|
105
|
+
where: (0, _objectSpread2.default)({
|
|
106
|
+
tenant,
|
|
107
|
+
locale
|
|
108
|
+
}, where),
|
|
109
|
+
sort: ["createdOn_ASC"],
|
|
110
|
+
limit,
|
|
111
|
+
after
|
|
112
|
+
});
|
|
113
|
+
} catch (error) {
|
|
114
|
+
throw _error.default.from(error, {
|
|
115
|
+
message: "Could not list links.",
|
|
116
|
+
code: "LIST_LINKS_ERROR",
|
|
117
|
+
data: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, where), {}, {
|
|
118
|
+
limit,
|
|
119
|
+
after
|
|
120
|
+
})
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
async createLink(input) {
|
|
126
|
+
await createSchema.validate(input);
|
|
127
|
+
const tenant = getTenantId();
|
|
128
|
+
const locale = getLocaleCode();
|
|
129
|
+
const existing = await storageOperations.getLink({
|
|
130
|
+
tenant,
|
|
131
|
+
locale,
|
|
132
|
+
folderId: input.folderId,
|
|
133
|
+
id: input.id
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
if (existing) {
|
|
137
|
+
throw new _error.default(`Link with id "${input.id}" already exists.`, "LINK_EXISTS");
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const identity = getIdentity();
|
|
141
|
+
const link = (0, _objectSpread2.default)((0, _objectSpread2.default)({
|
|
142
|
+
linkId: (0, _mdbid.default)(),
|
|
143
|
+
tenant,
|
|
144
|
+
locale
|
|
145
|
+
}, input), {}, {
|
|
146
|
+
webinyVersion: process.env.WEBINY_VERSION,
|
|
147
|
+
createdOn: new Date().toISOString(),
|
|
148
|
+
createdBy: {
|
|
149
|
+
id: identity.id,
|
|
150
|
+
displayName: identity.displayName,
|
|
151
|
+
type: identity.type
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
await onLinkBeforeCreate.publish({
|
|
157
|
+
link
|
|
158
|
+
});
|
|
159
|
+
const result = await storageOperations.createLink({
|
|
160
|
+
link
|
|
161
|
+
});
|
|
162
|
+
await onLinkAfterCreate.publish({
|
|
163
|
+
link: result
|
|
164
|
+
});
|
|
165
|
+
return result;
|
|
166
|
+
} catch (error) {
|
|
167
|
+
throw _error.default.from(error, {
|
|
168
|
+
message: "Could not create link.",
|
|
169
|
+
code: "CREATE_LINK_ERROR",
|
|
170
|
+
data: (0, _objectSpread2.default)({}, input)
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
|
|
175
|
+
async updateLink(id, input) {
|
|
176
|
+
const tenant = getTenantId();
|
|
177
|
+
const locale = getLocaleCode();
|
|
178
|
+
const original = await storageOperations.getLink({
|
|
179
|
+
tenant,
|
|
180
|
+
locale,
|
|
181
|
+
id
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
if (!original) {
|
|
185
|
+
throw new _handlerGraphql.NotFoundError(`Link "${id}" was not found!`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
await updateSchema.validate(input);
|
|
189
|
+
const link = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, original), input);
|
|
190
|
+
|
|
191
|
+
try {
|
|
192
|
+
await onLinkBeforeUpdate.publish({
|
|
193
|
+
link,
|
|
194
|
+
original
|
|
195
|
+
});
|
|
196
|
+
const result = await storageOperations.updateLink({
|
|
197
|
+
original,
|
|
198
|
+
link
|
|
199
|
+
});
|
|
200
|
+
await onLinkAfterUpdate.publish({
|
|
201
|
+
link: result,
|
|
202
|
+
original
|
|
203
|
+
});
|
|
204
|
+
return result;
|
|
205
|
+
} catch (error) {
|
|
206
|
+
throw new _error.default(error.message || "Could not update link.", error.code || "UPDATE_LINK_ERROR", {
|
|
207
|
+
link
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
|
|
212
|
+
async deleteLink(id) {
|
|
213
|
+
const tenant = getTenantId();
|
|
214
|
+
const locale = getLocaleCode();
|
|
215
|
+
const link = await storageOperations.getLink({
|
|
216
|
+
tenant,
|
|
217
|
+
locale,
|
|
218
|
+
id
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
if (!link) {
|
|
222
|
+
throw new _handlerGraphql.NotFoundError(`Link "${id}" was not found!`);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
try {
|
|
226
|
+
await onLinkBeforeDelete.publish({
|
|
227
|
+
link
|
|
228
|
+
});
|
|
229
|
+
const result = await storageOperations.deleteLink({
|
|
230
|
+
link
|
|
231
|
+
});
|
|
232
|
+
await onLinkAfterDelete.publish({
|
|
233
|
+
link
|
|
234
|
+
});
|
|
235
|
+
return result;
|
|
236
|
+
} catch (error) {
|
|
237
|
+
throw new _error.default(error.message || "Could not delete link.", error.code || "DELETE_LINK_ERROR", {
|
|
238
|
+
link
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
|
|
243
|
+
async deleteLinks(folderIds) {
|
|
244
|
+
const tenant = getTenantId();
|
|
245
|
+
const locale = getLocaleCode();
|
|
246
|
+
|
|
247
|
+
try {
|
|
248
|
+
await onLinkBeforeDeleteBatch.publish({
|
|
249
|
+
folderIds
|
|
250
|
+
});
|
|
251
|
+
const result = await storageOperations.deleteLinks({
|
|
252
|
+
tenant,
|
|
253
|
+
locale,
|
|
254
|
+
folderIds
|
|
255
|
+
});
|
|
256
|
+
await onLinkAfterDeleteBatch.publish({
|
|
257
|
+
folderIds
|
|
258
|
+
});
|
|
259
|
+
return result;
|
|
260
|
+
} catch (error) {
|
|
261
|
+
throw new _error.default(error.message || "Could not batch delete links.", error.code || "DELETE_LINKS_ERROR", {
|
|
262
|
+
folderIds
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
};
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
exports.createLinksContext = createLinksContext;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["requiredString","joi","string","required","createSchema","object","id","folderId","tenant","locale","updateSchema","createLinksContext","getTenantId","getLocaleCode","getIdentity","storageOperations","onLinkBeforeCreate","createTopic","onLinkAfterCreate","onLinkBeforeUpdate","onLinkAfterUpdate","onLinkBeforeDelete","onLinkAfterDelete","onLinkBeforeDeleteBatch","onLinkAfterDeleteBatch","getLink","link","error","WebinyError","from","message","code","data","NotFoundError","listLinks","where","limit","after","sort","createLink","input","validate","existing","identity","linkId","mdbid","webinyVersion","process","env","WEBINY_VERSION","createdOn","Date","toISOString","createdBy","displayName","type","publish","result","updateLink","original","deleteLink","deleteLinks","folderIds"],"sources":["links.ts"],"sourcesContent":["/**\n * Package mdbid does not have types.\n */\n// @ts-ignore\nimport mdbid from \"mdbid\";\nimport WebinyError from \"@webiny/error\";\nimport { NotFoundError } from \"@webiny/handler-graphql\";\nimport { createTopic } from \"@webiny/pubsub\";\nimport joi from \"joi\";\n\nimport {\n FoldersConfig,\n ILinks,\n Link,\n LinkInput,\n ListLinksParams,\n ListLinksResponse,\n OnLinkAfterCreateTopicParams,\n OnLinkAfterDeleteBatchTopicParams,\n OnLinkAfterDeleteTopicParams,\n OnLinkAfterUpdateTopicParams,\n OnLinkBeforeCreateTopicParams,\n OnLinkBeforeDeleteBatchTopicParams,\n OnLinkBeforeDeleteTopicParams,\n OnLinkBeforeUpdateTopicParams\n} from \"~/types\";\n\nconst requiredString = joi.string().required();\n\nconst createSchema = joi.object({\n id: requiredString,\n folderId: requiredString,\n tenant: requiredString,\n locale: requiredString\n});\n\nconst updateSchema = joi.object({\n folderId: requiredString\n});\n\nexport const createLinksContext = async ({\n getTenantId,\n getLocaleCode,\n getIdentity,\n storageOperations\n}: FoldersConfig): Promise<ILinks> => {\n // create\n const onLinkBeforeCreate = createTopic<OnLinkBeforeCreateTopicParams>(\n \"folders.onLinkBeforeCreate\"\n );\n const onLinkAfterCreate = createTopic<OnLinkAfterCreateTopicParams>(\n \"folders.onLinkAfterCreate\"\n );\n // update\n const onLinkBeforeUpdate = createTopic<OnLinkBeforeUpdateTopicParams>(\n \"folders.onLinkBeforeUpdate\"\n );\n const onLinkAfterUpdate = createTopic<OnLinkAfterUpdateTopicParams>(\n \"folders.onLinkAfterUpdate\"\n );\n // delete\n const onLinkBeforeDelete = createTopic<OnLinkBeforeDeleteTopicParams>(\n \"folders.onLinkBeforeDelete\"\n );\n const onLinkAfterDelete = createTopic<OnLinkAfterDeleteTopicParams>(\n \"folders.onLinkAfterDelete\"\n );\n // delete batch\n const onLinkBeforeDeleteBatch = createTopic<OnLinkBeforeDeleteBatchTopicParams>(\n \"folders.onLinkBeforeDeleteBatch\"\n );\n const onLinkAfterDeleteBatch = createTopic<OnLinkAfterDeleteBatchTopicParams>(\n \"folders.onLinkAfterDeleteBatch\"\n );\n\n return {\n onLinkBeforeCreate,\n onLinkAfterCreate,\n onLinkBeforeUpdate,\n onLinkAfterUpdate,\n onLinkBeforeDelete,\n onLinkAfterDelete,\n onLinkBeforeDeleteBatch,\n onLinkAfterDeleteBatch,\n async getLink(id: string): Promise<Link> {\n const tenant = getTenantId();\n const locale = getLocaleCode();\n\n let link: Link | undefined;\n try {\n link = await storageOperations.getLink({ tenant, locale, id });\n } catch (error) {\n throw WebinyError.from(error, {\n message: \"Could not get link.\",\n code: \"GET_LINK_ERROR\",\n data: { id }\n });\n }\n if (!link) {\n throw new NotFoundError(`Unable to find link with id: ${id}`);\n }\n return link;\n },\n\n async listLinks({ where, limit, after }: ListLinksParams): Promise<ListLinksResponse> {\n const tenant = getTenantId();\n const locale = getLocaleCode();\n\n try {\n return await storageOperations.listLinks({\n where: { tenant, locale, ...where },\n sort: [\"createdOn_ASC\"],\n limit,\n after\n });\n } catch (error) {\n throw WebinyError.from(error, {\n message: \"Could not list links.\",\n code: \"LIST_LINKS_ERROR\",\n data: { ...where, limit, after }\n });\n }\n },\n\n async createLink(input: LinkInput): Promise<Link> {\n await createSchema.validate(input);\n\n const tenant = getTenantId();\n const locale = getLocaleCode();\n\n const existing = await storageOperations.getLink({\n tenant,\n locale,\n folderId: input.folderId,\n id: input.id\n });\n\n if (existing) {\n throw new WebinyError(`Link with id \"${input.id}\" already exists.`, \"LINK_EXISTS\");\n }\n\n const identity = getIdentity();\n\n const link: Link = {\n linkId: mdbid(),\n tenant,\n locale,\n ...input,\n webinyVersion: process.env.WEBINY_VERSION as string,\n createdOn: new Date().toISOString(),\n createdBy: {\n id: identity.id,\n displayName: identity.displayName,\n type: identity.type\n }\n };\n\n try {\n await onLinkBeforeCreate.publish({\n link\n });\n const result = await storageOperations.createLink({ link });\n await onLinkAfterCreate.publish({\n link: result\n });\n return result;\n } catch (error) {\n throw WebinyError.from(error, {\n message: \"Could not create link.\",\n code: \"CREATE_LINK_ERROR\",\n data: { ...input }\n });\n }\n },\n\n async updateLink(id: string, input: Record<string, any>): Promise<Link> {\n const tenant = getTenantId();\n const locale = getLocaleCode();\n\n const original = await storageOperations.getLink({ tenant, locale, id });\n\n if (!original) {\n throw new NotFoundError(`Link \"${id}\" was not found!`);\n }\n\n await updateSchema.validate(input);\n\n const link: Link = {\n ...original,\n ...input\n };\n\n try {\n await onLinkBeforeUpdate.publish({\n link,\n original\n });\n const result = await storageOperations.updateLink({ original, link });\n await onLinkAfterUpdate.publish({\n link: result,\n original\n });\n return result;\n } catch (error) {\n throw new WebinyError(\n error.message || \"Could not update link.\",\n error.code || \"UPDATE_LINK_ERROR\",\n {\n link\n }\n );\n }\n },\n\n async deleteLink(id: string): Promise<void> {\n const tenant = getTenantId();\n const locale = getLocaleCode();\n\n const link = await storageOperations.getLink({ tenant, locale, id });\n\n if (!link) {\n throw new NotFoundError(`Link \"${id}\" was not found!`);\n }\n\n try {\n await onLinkBeforeDelete.publish({\n link\n });\n const result = await storageOperations.deleteLink({ link });\n await onLinkAfterDelete.publish({\n link\n });\n return result;\n } catch (error) {\n throw new WebinyError(\n error.message || \"Could not delete link.\",\n error.code || \"DELETE_LINK_ERROR\",\n {\n link\n }\n );\n }\n },\n\n async deleteLinks(folderIds: string[]): Promise<void> {\n const tenant = getTenantId();\n const locale = getLocaleCode();\n\n try {\n await onLinkBeforeDeleteBatch.publish({\n folderIds\n });\n const result = await storageOperations.deleteLinks({ tenant, locale, folderIds });\n await onLinkAfterDeleteBatch.publish({\n folderIds\n });\n return result;\n } catch (error) {\n throw new WebinyError(\n error.message || \"Could not batch delete links.\",\n error.code || \"DELETE_LINKS_ERROR\",\n {\n folderIds\n }\n );\n }\n }\n };\n};\n"],"mappings":";;;;;;;;;;;AAIA;;AACA;;AACA;;AACA;;AACA;;AARA;AACA;AACA;AACA;AAwBA,MAAMA,cAAc,GAAGC,YAAA,CAAIC,MAAJ,GAAaC,QAAb,EAAvB;;AAEA,MAAMC,YAAY,GAAGH,YAAA,CAAII,MAAJ,CAAW;EAC5BC,EAAE,EAAEN,cADwB;EAE5BO,QAAQ,EAAEP,cAFkB;EAG5BQ,MAAM,EAAER,cAHoB;EAI5BS,MAAM,EAAET;AAJoB,CAAX,CAArB;;AAOA,MAAMU,YAAY,GAAGT,YAAA,CAAII,MAAJ,CAAW;EAC5BE,QAAQ,EAAEP;AADkB,CAAX,CAArB;;AAIO,MAAMW,kBAAkB,GAAG,OAAO;EACrCC,WADqC;EAErCC,aAFqC;EAGrCC,WAHqC;EAIrCC;AAJqC,CAAP,KAKI;EAClC;EACA,MAAMC,kBAAkB,GAAG,IAAAC,mBAAA,EACvB,4BADuB,CAA3B;EAGA,MAAMC,iBAAiB,GAAG,IAAAD,mBAAA,EACtB,2BADsB,CAA1B,CALkC,CAQlC;;EACA,MAAME,kBAAkB,GAAG,IAAAF,mBAAA,EACvB,4BADuB,CAA3B;EAGA,MAAMG,iBAAiB,GAAG,IAAAH,mBAAA,EACtB,2BADsB,CAA1B,CAZkC,CAelC;;EACA,MAAMI,kBAAkB,GAAG,IAAAJ,mBAAA,EACvB,4BADuB,CAA3B;EAGA,MAAMK,iBAAiB,GAAG,IAAAL,mBAAA,EACtB,2BADsB,CAA1B,CAnBkC,CAsBlC;;EACA,MAAMM,uBAAuB,GAAG,IAAAN,mBAAA,EAC5B,iCAD4B,CAAhC;EAGA,MAAMO,sBAAsB,GAAG,IAAAP,mBA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