(ctx: PluginCtx<ToolkitStorage>)
| 274 | }); |
| 275 | |
| 276 | const makeToolkitsExtension = (ctx: PluginCtx<ToolkitStorage>) => { |
| 277 | const storage = ctx.storage; |
| 278 | |
| 279 | const list = () => |
| 280 | storage.toolkits |
| 281 | .query({ orderBy: [{ field: "name" }] }) |
| 282 | .pipe( |
| 283 | Effect.map((entries) => |
| 284 | entries |
| 285 | .map(toolkitToResponse) |
| 286 | .sort( |
| 287 | (a, b) => |
| 288 | (a.owner === b.owner ? 0 : a.owner === "org" ? -1 : 1) || |
| 289 | a.name.localeCompare(b.name) || |
| 290 | a.slug.localeCompare(b.slug), |
| 291 | ), |
| 292 | ), |
| 293 | ); |
| 294 | |
| 295 | const getEntry = (toolkitId: string) => storage.toolkits.get({ key: toolkitId }); |
| 296 | |
| 297 | const getBySlugEntry = (slug: string) => |
| 298 | storage.toolkits |
| 299 | .query({ where: { slug } }) |
| 300 | .pipe( |
| 301 | Effect.map( |
| 302 | (entries) => entries.find((entry) => entry.owner === "org") ?? entries[0] ?? null, |
| 303 | ), |
| 304 | ); |
| 305 | |
| 306 | const requireToolkit = (toolkitId: string) => |
| 307 | getEntry(toolkitId).pipe( |
| 308 | Effect.flatMap((entry) => (entry ? Effect.succeed(entry) : fail("Toolkit not found."))), |
| 309 | ); |
| 310 | |
| 311 | const assertSlugAvailable = (slug: string, ignoreToolkitId?: string) => |
| 312 | storage.toolkits.query({ where: { slug } }).pipe( |
| 313 | Effect.flatMap((entries) => { |
| 314 | const collision = entries.find((entry) => entry.data.id !== ignoreToolkitId); |
| 315 | return collision ? fail(`Toolkit slug "${slug}" is already in use.`) : Effect.void; |
| 316 | }), |
| 317 | ); |
| 318 | |
| 319 | const listPoliciesForRecord = (toolkitId: string) => |
| 320 | storage.policies |
| 321 | .query({ where: { toolkitId } }) |
| 322 | .pipe(Effect.map((entries) => entries.map((entry) => entry.data).sort(comparePositioned))); |
| 323 | |
| 324 | const listConnectionsForRecord = (toolkitId: string) => |
| 325 | storage.connections |
| 326 | .query({ where: { toolkitId } }) |
| 327 | .pipe(Effect.map((entries) => entries.map((entry) => entry.data).sort(comparePositioned))); |
| 328 | |
| 329 | const requirePolicy = (toolkitId: string, policyId: string, owner: Owner) => |
| 330 | storage.policies |
| 331 | .getForOwner({ owner, key: policyId }) |
| 332 | .pipe( |
| 333 | Effect.flatMap((entry) => |
no test coverage detected