From b1841a53add4abf1545be99a24c1955af511cedd Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Mon, 8 Jan 2024 15:16:30 +0100 Subject: [PATCH] update folder mocks and handlers to new endpoints --- .../file-system/file-system-folder.manager.ts | 26 ++++++++++------ .../handlers/partial-view/detail.handlers.ts | 2 +- .../handlers/partial-view/folder.handlers.ts | 30 +++++++++++------- .../mocks/handlers/partial-views.handlers.ts | 2 +- .../mocks/handlers/script/detail.handlers.ts | 2 +- .../mocks/handlers/script/folder.handlers.ts | 30 +++++++++++------- .../handlers/stylesheet/detail.handlers.ts | 2 +- .../handlers/stylesheet/folder.handlers.ts | 31 ++++++++++++------- .../script-folder.server.data-source.ts | 14 ++++----- .../stylesheet-folder.server.data-source.ts | 14 ++++----- 10 files changed, 88 insertions(+), 65 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/mocks/data/file-system/file-system-folder.manager.ts b/src/Umbraco.Web.UI.Client/src/mocks/data/file-system/file-system-folder.manager.ts index b3d6e8cd63..0906e794ed 100644 --- a/src/Umbraco.Web.UI.Client/src/mocks/data/file-system/file-system-folder.manager.ts +++ b/src/Umbraco.Web.UI.Client/src/mocks/data/file-system/file-system-folder.manager.ts @@ -1,8 +1,11 @@ import { UmbFileSystemMockDbBase } from './file-system-base.js'; -import { CreatePathFolderRequestModel, PathFolderModelBaseModel } from '@umbraco-cms/backoffice/backend-api'; +import { + FileSystemCreateRequestModelBaseModel, + FileSystemResponseModelBaseModel, +} from '@umbraco-cms/backoffice/backend-api'; export class UmbMockFileSystemFolderManager< - MockItemType extends PathFolderModelBaseModel & { path: string; isFolder: boolean }, + MockItemType extends FileSystemResponseModelBaseModel & { isFolder: boolean }, > { #db: UmbFileSystemMockDbBase; @@ -10,17 +13,21 @@ export class UmbMockFileSystemFolderManager< this.#db = db; } - create(request: CreatePathFolderRequestModel) { - const newFolder: MockItemType = { - path: request.parentPath ? `${request.parentPath}/${request.name}` : request.name, - parenPath: request.parentPath || null, + create(request: FileSystemCreateRequestModelBaseModel) { + const path = request.parentPath ? `${request.parentPath}/${request.name}` : request.name; + + const newFolder = { + path, + parent: request.parentPath ? { path: request.parentPath } : null, name: request.name, hasChildren: false, isFolder: true, content: '', - }; + } as unknown as MockItemType; this.#db.create(newFolder); + + return path; } read(path: string) { @@ -39,10 +46,11 @@ export class UmbMockFileSystemFolderManager< } } - #defaultReadMapper = (item: MockItemType): PathFolderModelBaseModel & { path: string } => { + #defaultReadMapper = (item: MockItemType): FileSystemResponseModelBaseModel => { return { - name: item.name, path: item.path, + name: item.name, + parent: item.parent, }; }; } diff --git a/src/Umbraco.Web.UI.Client/src/mocks/handlers/partial-view/detail.handlers.ts b/src/Umbraco.Web.UI.Client/src/mocks/handlers/partial-view/detail.handlers.ts index 3bcb2f4bd7..1868dd7112 100644 --- a/src/Umbraco.Web.UI.Client/src/mocks/handlers/partial-view/detail.handlers.ts +++ b/src/Umbraco.Web.UI.Client/src/mocks/handlers/partial-view/detail.handlers.ts @@ -10,7 +10,7 @@ export const detailHandlers = [ if (!requestBody) return res(ctx.status(400, 'no body found')); const path = umbPartialViewMockDB.file.create(requestBody); return res( - ctx.status(200), + ctx.status(201), ctx.set({ Location: path, }), diff --git a/src/Umbraco.Web.UI.Client/src/mocks/handlers/partial-view/folder.handlers.ts b/src/Umbraco.Web.UI.Client/src/mocks/handlers/partial-view/folder.handlers.ts index 1df3ab0547..a26bc44205 100644 --- a/src/Umbraco.Web.UI.Client/src/mocks/handlers/partial-view/folder.handlers.ts +++ b/src/Umbraco.Web.UI.Client/src/mocks/handlers/partial-view/folder.handlers.ts @@ -1,26 +1,32 @@ const { rest } = window.MockServiceWorker; import { umbPartialViewMockDB } from '../../data/partial-view/partial-view.db.js'; import { UMB_SLUG } from './slug.js'; -import { CreatePathFolderRequestModel } from '@umbraco-cms/backoffice/backend-api'; +import { CreatePartialViewFolderRequestModel } from '@umbraco-cms/backoffice/backend-api'; import { umbracoPath } from '@umbraco-cms/backoffice/utils'; export const folderHandlers = [ - rest.get(umbracoPath(`${UMB_SLUG}/folder`), (req, res, ctx) => { - const path = req.url.searchParams.get('path'); + rest.post(umbracoPath(`${UMB_SLUG}/folder`), async (req, res, ctx) => { + const requestBody = (await req.json()) as CreatePartialViewFolderRequestModel; + if (!requestBody) return res(ctx.status(400, 'no body found')); + const path = umbPartialViewMockDB.folder.create(requestBody); + + return res( + ctx.status(201), + ctx.set({ + Location: path, + }), + ); + }), + + rest.get(umbracoPath(`${UMB_SLUG}/folder/:path`), (req, res, ctx) => { + const path = req.params.path as string; if (!path) return res(ctx.status(400)); const response = umbPartialViewMockDB.folder.read(path); return res(ctx.status(200), ctx.json(response)); }), - rest.post(umbracoPath(`${UMB_SLUG}/folder`), async (req, res, ctx) => { - const requestBody = (await req.json()) as CreatePathFolderRequestModel; - if (!requestBody) return res(ctx.status(400, 'no body found')); - umbPartialViewMockDB.folder.create(requestBody); - return res(ctx.status(200)); - }), - - rest.delete(umbracoPath(`${UMB_SLUG}/folder`), (req, res, ctx) => { - const path = req.url.searchParams.get('path'); + rest.delete(umbracoPath(`${UMB_SLUG}/folder/:path`), (req, res, ctx) => { + const path = req.params.path as string; if (!path) return res(ctx.status(400)); umbPartialViewMockDB.folder.delete(path); return res(ctx.status(200)); diff --git a/src/Umbraco.Web.UI.Client/src/mocks/handlers/partial-views.handlers.ts b/src/Umbraco.Web.UI.Client/src/mocks/handlers/partial-views.handlers.ts index ac30c06171..e75718b632 100644 --- a/src/Umbraco.Web.UI.Client/src/mocks/handlers/partial-views.handlers.ts +++ b/src/Umbraco.Web.UI.Client/src/mocks/handlers/partial-views.handlers.ts @@ -31,7 +31,7 @@ const detailHandlers: RestHandler>[] = [ if (!requestBody) return res(ctx.status(400, 'no body found')); const path = umbPartialViewMockDB.file.create(requestBody); return res( - ctx.status(200), + ctx.status(201), ctx.set({ Location: path, }), diff --git a/src/Umbraco.Web.UI.Client/src/mocks/handlers/script/detail.handlers.ts b/src/Umbraco.Web.UI.Client/src/mocks/handlers/script/detail.handlers.ts index 8c19307adc..06d4affa2e 100644 --- a/src/Umbraco.Web.UI.Client/src/mocks/handlers/script/detail.handlers.ts +++ b/src/Umbraco.Web.UI.Client/src/mocks/handlers/script/detail.handlers.ts @@ -10,7 +10,7 @@ export const detailHandlers = [ if (!requestBody) return res(ctx.status(400, 'no body found')); const path = umbScriptMockDb.file.create(requestBody); return res( - ctx.status(200), + ctx.status(201), ctx.set({ Location: path, }), diff --git a/src/Umbraco.Web.UI.Client/src/mocks/handlers/script/folder.handlers.ts b/src/Umbraco.Web.UI.Client/src/mocks/handlers/script/folder.handlers.ts index 7d07a1a6e9..7e17b40806 100644 --- a/src/Umbraco.Web.UI.Client/src/mocks/handlers/script/folder.handlers.ts +++ b/src/Umbraco.Web.UI.Client/src/mocks/handlers/script/folder.handlers.ts @@ -1,26 +1,32 @@ const { rest } = window.MockServiceWorker; import { umbScriptMockDb } from '../../data/script/script.db.js'; import { UMB_SLUG } from './slug.js'; -import { CreatePathFolderRequestModel } from '@umbraco-cms/backoffice/backend-api'; +import { CreateScriptFolderRequestModel } from '@umbraco-cms/backoffice/backend-api'; import { umbracoPath } from '@umbraco-cms/backoffice/utils'; export const folderHandlers = [ - rest.get(umbracoPath(`${UMB_SLUG}/folder`), (req, res, ctx) => { - const path = req.url.searchParams.get('path'); + rest.post(umbracoPath(`${UMB_SLUG}/folder`), async (req, res, ctx) => { + const requestBody = (await req.json()) as CreateScriptFolderRequestModel; + if (!requestBody) return res(ctx.status(400, 'no body found')); + const path = umbScriptMockDb.folder.create(requestBody); + + return res( + ctx.status(201), + ctx.set({ + Location: path, + }), + ); + }), + + rest.get(umbracoPath(`${UMB_SLUG}/folder/:path`), (req, res, ctx) => { + const path = req.params.path as string; if (!path) return res(ctx.status(400)); const response = umbScriptMockDb.folder.read(path); return res(ctx.status(200), ctx.json(response)); }), - rest.post(umbracoPath(`${UMB_SLUG}/folder`), async (req, res, ctx) => { - const requestBody = (await req.json()) as CreatePathFolderRequestModel; - if (!requestBody) return res(ctx.status(400, 'no body found')); - umbScriptMockDb.folder.create(requestBody); - return res(ctx.status(200)); - }), - - rest.delete(umbracoPath(`${UMB_SLUG}/folder`), (req, res, ctx) => { - const path = req.url.searchParams.get('path'); + rest.delete(umbracoPath(`${UMB_SLUG}/folder/:path`), (req, res, ctx) => { + const path = req.params.path as string; if (!path) return res(ctx.status(400)); umbScriptMockDb.folder.delete(path); return res(ctx.status(200)); diff --git a/src/Umbraco.Web.UI.Client/src/mocks/handlers/stylesheet/detail.handlers.ts b/src/Umbraco.Web.UI.Client/src/mocks/handlers/stylesheet/detail.handlers.ts index 2132868379..2a5a0f249e 100644 --- a/src/Umbraco.Web.UI.Client/src/mocks/handlers/stylesheet/detail.handlers.ts +++ b/src/Umbraco.Web.UI.Client/src/mocks/handlers/stylesheet/detail.handlers.ts @@ -10,7 +10,7 @@ export const detailHandlers = [ if (!requestBody) return res(ctx.status(400, 'no body found')); const path = umbStylesheetData.file.create(requestBody); return res( - ctx.status(200), + ctx.status(201), ctx.set({ Location: path, }), diff --git a/src/Umbraco.Web.UI.Client/src/mocks/handlers/stylesheet/folder.handlers.ts b/src/Umbraco.Web.UI.Client/src/mocks/handlers/stylesheet/folder.handlers.ts index cec27b6a39..1badd6e735 100644 --- a/src/Umbraco.Web.UI.Client/src/mocks/handlers/stylesheet/folder.handlers.ts +++ b/src/Umbraco.Web.UI.Client/src/mocks/handlers/stylesheet/folder.handlers.ts @@ -1,26 +1,33 @@ const { rest } = window.MockServiceWorker; import { umbStylesheetData } from '../../data/stylesheet/stylesheet.db.js'; import { UMB_SLUG } from './slug.js'; -import { CreatePathFolderRequestModel } from '@umbraco-cms/backoffice/backend-api'; +import { CreateStylesheetFolderRequestModel } from '@umbraco-cms/backoffice/backend-api'; import { umbracoPath } from '@umbraco-cms/backoffice/utils'; export const folderHandlers = [ - rest.get(umbracoPath(`${UMB_SLUG}/folder`), (req, res, ctx) => { - const path = req.url.searchParams.get('path'); + rest.post(umbracoPath(`${UMB_SLUG}/folder`), async (req, res, ctx) => { + const requestBody = (await req.json()) as CreateStylesheetFolderRequestModel; + if (!requestBody) return res(ctx.status(400, 'no body found')); + + const path = umbStylesheetData.folder.create(requestBody); + + return res( + ctx.status(201), + ctx.set({ + Location: path, + }), + ); + }), + + rest.get(umbracoPath(`${UMB_SLUG}/folder/:path`), (req, res, ctx) => { + const path = req.params.path as string; if (!path) return res(ctx.status(400)); const response = umbStylesheetData.folder.read(path); return res(ctx.status(200), ctx.json(response)); }), - rest.post(umbracoPath(`${UMB_SLUG}/folder`), async (req, res, ctx) => { - const requestBody = (await req.json()) as CreatePathFolderRequestModel; - if (!requestBody) return res(ctx.status(400, 'no body found')); - umbStylesheetData.folder.create(requestBody); - return res(ctx.status(200)); - }), - - rest.delete(umbracoPath(`${UMB_SLUG}/folder`), (req, res, ctx) => { - const path = req.url.searchParams.get('path'); + rest.delete(umbracoPath(`${UMB_SLUG}/folder/:path`), (req, res, ctx) => { + const path = req.params.path as string; if (!path) return res(ctx.status(400)); umbStylesheetData.folder.delete(path); return res(ctx.status(200)); diff --git a/src/Umbraco.Web.UI.Client/src/packages/templating/scripts/tree/folder/script-folder.server.data-source.ts b/src/Umbraco.Web.UI.Client/src/packages/templating/scripts/tree/folder/script-folder.server.data-source.ts index 2e76ec7de3..65831243ac 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/templating/scripts/tree/folder/script-folder.server.data-source.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/templating/scripts/tree/folder/script-folder.server.data-source.ts @@ -36,7 +36,7 @@ export class UmbScriptFolderServerDataSource implements UmbFolderDataSource { const { data, error } = await tryExecuteAndNotify( this.#host, - ScriptResource.getScriptFolder({ + ScriptResource.getScriptFolderByPath({ path, }), ); @@ -44,7 +44,7 @@ export class UmbScriptFolderServerDataSource implements UmbFolderDataSource { if (data) { const mappedData = { unique: this.#serverPathUniqueSerializer.toUnique(data.path), - parentUnique: this.#serverPathUniqueSerializer.toParentUnique(data.path), + parentUnique: data.parent ? this.#serverPathUniqueSerializer.toUnique(data.parent.path) : null, name: data.name, }; @@ -71,17 +71,15 @@ export class UmbScriptFolderServerDataSource implements UmbFolderDataSource { name: args.name, }; - const { error } = await tryExecuteAndNotify( + const { data, error } = await tryExecuteAndNotify( this.#host, ScriptResource.postScriptFolder({ requestBody, }), ); - if (!error) { - /* TODO: investigate why we don't get the location header as part of data, - so we don't have to construct the path ourselves */ - const newPath = parentPath ? `${parentPath}/${args.name}` : args.name; + if (data) { + const newPath = this.#serverPathUniqueSerializer.toUnique(data); return this.read(newPath); } @@ -101,7 +99,7 @@ export class UmbScriptFolderServerDataSource implements UmbFolderDataSource { return tryExecuteAndNotify( this.#host, - ScriptResource.deleteScriptFolder({ + ScriptResource.deleteScriptFolderByPath({ path, }), ); diff --git a/src/Umbraco.Web.UI.Client/src/packages/templating/stylesheets/tree/folder/stylesheet-folder.server.data-source.ts b/src/Umbraco.Web.UI.Client/src/packages/templating/stylesheets/tree/folder/stylesheet-folder.server.data-source.ts index a958d2aaf8..e0d531f56b 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/templating/stylesheets/tree/folder/stylesheet-folder.server.data-source.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/templating/stylesheets/tree/folder/stylesheet-folder.server.data-source.ts @@ -36,7 +36,7 @@ export class UmbStylesheetFolderServerDataSource implements UmbFolderDataSource const { data, error } = await tryExecuteAndNotify( this.#host, - StylesheetResource.getStylesheetFolder({ + StylesheetResource.getStylesheetFolderByPath({ path, }), ); @@ -44,7 +44,7 @@ export class UmbStylesheetFolderServerDataSource implements UmbFolderDataSource if (data) { const mappedData = { unique: this.#serverPathUniqueSerializer.toUnique(data.path), - parentUnique: this.#serverPathUniqueSerializer.toParentUnique(data.path), + parentUnique: data.parent ? this.#serverPathUniqueSerializer.toUnique(data.parent.path) : null, name: data.name, }; @@ -71,17 +71,15 @@ export class UmbStylesheetFolderServerDataSource implements UmbFolderDataSource name: args.name, }; - const { error } = await tryExecuteAndNotify( + const { data, error } = await tryExecuteAndNotify( this.#host, StylesheetResource.postStylesheetFolder({ requestBody, }), ); - if (!error) { - /* TODO: investigate why we don't get the location header as part of data, - so we don't have to construct the path ourselves */ - const newPath = parentPath ? `${parentPath}/${args.name}` : args.name; + if (data) { + const newPath = this.#serverPathUniqueSerializer.toUnique(data); return this.read(newPath); } @@ -101,7 +99,7 @@ export class UmbStylesheetFolderServerDataSource implements UmbFolderDataSource return tryExecuteAndNotify( this.#host, - StylesheetResource.deleteStylesheetFolder({ + StylesheetResource.deleteStylesheetFolderByPath({ path, }), );