update request handlers
This commit is contained in:
@@ -1,50 +1,48 @@
|
||||
import { UmbEntityData } from './entity.data.js';
|
||||
import { createFileSystemTreeItem, textFileItemMapper } from './utils.js';
|
||||
import {
|
||||
CreateTextFileViewModelBaseModel,
|
||||
FileSystemTreeItemPresentationModel,
|
||||
PagedFileSystemTreeItemPresentationModel,
|
||||
PartialViewItemResponseModel,
|
||||
PartialViewResponseModel,
|
||||
PartialViewSnippetResponseModel,
|
||||
SnippetItemResponseModel,
|
||||
} from '@umbraco-cms/backoffice/backend-api';
|
||||
|
||||
type PartialViewsDataItem = PartialViewResponseModel & FileSystemTreeItemPresentationModel & { id: string };
|
||||
export type UmbMockPartialViewModelHack = PartialViewResponseModel &
|
||||
FileSystemTreeItemPresentationModel &
|
||||
PartialViewItemResponseModel;
|
||||
|
||||
export const treeData: Array<PartialViewsDataItem> = [
|
||||
export interface UmbMockPartialViewModel extends Omit<UmbMockPartialViewModelHack, 'type' | 'icon'> {
|
||||
parentPath: string | null;
|
||||
}
|
||||
|
||||
export const data: Array<UmbMockPartialViewModel> = [
|
||||
{
|
||||
id: 'blockgrid',
|
||||
path: 'blockgrid',
|
||||
isFolder: true,
|
||||
name: 'blockgrid',
|
||||
type: 'partial-view',
|
||||
path: 'blockgrid',
|
||||
parentPath: null,
|
||||
isFolder: true,
|
||||
hasChildren: true,
|
||||
content: '',
|
||||
},
|
||||
{
|
||||
id: 'blocklist',
|
||||
path: 'blocklist',
|
||||
isFolder: true,
|
||||
name: 'blocklist',
|
||||
type: 'partial-view',
|
||||
hasChildren: true,
|
||||
content: '',
|
||||
},
|
||||
{
|
||||
id: 'grid',
|
||||
path: 'grid',
|
||||
path: 'blocklist',
|
||||
parentPath: null,
|
||||
isFolder: true,
|
||||
hasChildren: true,
|
||||
content: '',
|
||||
},
|
||||
{
|
||||
name: 'grid',
|
||||
path: 'grid',
|
||||
parentPath: null,
|
||||
isFolder: true,
|
||||
name: 'grid',
|
||||
type: 'partial-view',
|
||||
hasChildren: true,
|
||||
content: '',
|
||||
},
|
||||
{
|
||||
id: 'blockgrid/area.cshtml',
|
||||
path: 'blockgrid/area.cshtml',
|
||||
isFolder: false,
|
||||
name: 'area.cshtml',
|
||||
type: 'partial-view',
|
||||
path: 'blockgrid/area.cshtml',
|
||||
parentPath: 'blockgrid',
|
||||
isFolder: false,
|
||||
hasChildren: false,
|
||||
content: `@using Umbraco.Extensions
|
||||
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Umbraco.Cms.Core.Models.Blocks.BlockGridArea>
|
||||
@@ -59,20 +57,18 @@ export const treeData: Array<PartialViewsDataItem> = [
|
||||
`,
|
||||
},
|
||||
{
|
||||
id: 'blockgrid/items.cshtml',
|
||||
path: 'blockgrid/items.cshtml',
|
||||
isFolder: false,
|
||||
name: 'items.cshtml',
|
||||
type: 'partial-view',
|
||||
path: 'blockgrid/items.cshtml',
|
||||
parentPath: 'blockgrid',
|
||||
isFolder: false,
|
||||
hasChildren: false,
|
||||
content: '@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage',
|
||||
},
|
||||
{
|
||||
id: 'blocklist/default.cshtml',
|
||||
path: 'blocklist/default.cshtml',
|
||||
isFolder: false,
|
||||
name: 'default.cshtml',
|
||||
type: 'partial-view',
|
||||
path: 'blocklist/default.cshtml',
|
||||
parentPath: 'blocklist',
|
||||
isFolder: false,
|
||||
hasChildren: false,
|
||||
content: `@using Umbraco.Extensions
|
||||
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Umbraco.Cms.Core.Models.Blocks.BlockGridModel>
|
||||
@@ -88,11 +84,10 @@ export const treeData: Array<PartialViewsDataItem> = [
|
||||
`,
|
||||
},
|
||||
{
|
||||
id: 'grid/embed.cshtm',
|
||||
path: 'grid/embed.cshtm',
|
||||
isFolder: false,
|
||||
name: 'embed.cshtml',
|
||||
type: 'partial-view',
|
||||
path: 'grid/embed.cshtm',
|
||||
parentPath: 'grid',
|
||||
isFolder: false,
|
||||
hasChildren: false,
|
||||
content: `@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<dynamic>
|
||||
|
||||
@@ -108,43 +103,15 @@ export const treeData: Array<PartialViewsDataItem> = [
|
||||
`,
|
||||
},
|
||||
{
|
||||
id: 'grid/default.cshtml',
|
||||
name: 'default.cshtml',
|
||||
path: 'grid/default.cshtml',
|
||||
parentPath: 'grid',
|
||||
isFolder: false,
|
||||
name: 'items.cshtml',
|
||||
type: 'partial-view',
|
||||
hasChildren: false,
|
||||
content: '@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage',
|
||||
},
|
||||
];
|
||||
|
||||
class UmbPartialViewsTreeData extends UmbEntityData<FileSystemTreeItemPresentationModel> {
|
||||
constructor() {
|
||||
super(treeData);
|
||||
}
|
||||
|
||||
getTreeRoot(): PagedFileSystemTreeItemPresentationModel {
|
||||
const items = this.data.filter((item) => item.path?.includes('/') === false);
|
||||
const treeItems = items.map((item) => createFileSystemTreeItem(item));
|
||||
const total = items.length;
|
||||
return { items: treeItems, total };
|
||||
}
|
||||
|
||||
getTreeItemChildren(parentPath: string): PagedFileSystemTreeItemPresentationModel {
|
||||
const items = this.data.filter((item) => item.path?.startsWith(parentPath + '/'));
|
||||
const treeItems = items.map((item) => createFileSystemTreeItem(item));
|
||||
const total = items.length;
|
||||
return { items: treeItems, total };
|
||||
}
|
||||
|
||||
getTreeItem(paths: Array<string>): Array<FileSystemTreeItemPresentationModel> {
|
||||
const items = this.data.filter((item) => paths.includes(item.path ?? ''));
|
||||
return items.map((item) => createFileSystemTreeItem(item));
|
||||
}
|
||||
}
|
||||
|
||||
export const umbPartialViewsTreeData = new UmbPartialViewsTreeData();
|
||||
|
||||
export const snippets: Array<PartialViewSnippetResponseModel> = [
|
||||
{
|
||||
name: 'Empty',
|
||||
@@ -338,44 +305,3 @@ export const snippets: Array<PartialViewSnippetResponseModel> = [
|
||||
content: 'SiteMap',
|
||||
},
|
||||
];
|
||||
|
||||
class UmbPartialViewSnippetsData extends UmbEntityData<SnippetItemResponseModel> {
|
||||
constructor() {
|
||||
super(snippets);
|
||||
}
|
||||
|
||||
getSnippets(): Array<SnippetItemResponseModel> {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
getSnippetByName(name: string): SnippetItemResponseModel | undefined {
|
||||
return this.data.find((item) => item.name === name);
|
||||
}
|
||||
}
|
||||
|
||||
class UmbPartialViewsData extends UmbEntityData<PartialViewResponseModel> {
|
||||
constructor() {
|
||||
super(treeData);
|
||||
}
|
||||
|
||||
getPartialView(path: string): PartialViewResponseModel | undefined {
|
||||
return textFileItemMapper(this.data.find((item) => item.path === path));
|
||||
}
|
||||
|
||||
insertPartialView(item: CreateTextFileViewModelBaseModel) {
|
||||
const newItem: PartialViewsDataItem = {
|
||||
...item,
|
||||
path: `${item.parentPath}/${item.name}.cshtml}`,
|
||||
id: `${item.parentPath}/${item.name}.cshtml}`,
|
||||
isFolder: false,
|
||||
hasChildren: false,
|
||||
type: 'partial-view',
|
||||
};
|
||||
|
||||
this.insert(newItem);
|
||||
return newItem;
|
||||
}
|
||||
}
|
||||
|
||||
export const umbPartialViewSnippetsData = new UmbPartialViewSnippetsData();
|
||||
export const umbPartialViewsData = new UmbPartialViewsData();
|
||||
@@ -0,0 +1,44 @@
|
||||
import { UmbFileSystemMockDbBase } from '../file-system/file-system-base.js';
|
||||
import { UmbMockFileSystemFolderManager } from '../file-system/file-system-folder.manager.js';
|
||||
import { UmbMockFileSystemItemManager } from '../file-system/file-system-item.manager.js';
|
||||
import { UmbMockFileSystemTreeManager } from '../file-system/file-system-tree.manager.js';
|
||||
import { UmbMockFileSystemDetailManager } from '../file-system/file-system-detail.manager.js';
|
||||
import { UmbMockPartialViewModel, data } from './partial-view.data.js';
|
||||
import { CreatePartialViewRequestModel, PartialViewResponseModel } from '@umbraco-cms/backoffice/backend-api';
|
||||
|
||||
class UmbPartialViewMockDB extends UmbFileSystemMockDbBase<UmbMockPartialViewModel> {
|
||||
tree = new UmbMockFileSystemTreeManager<UmbMockPartialViewModel>(this);
|
||||
folder = new UmbMockFileSystemFolderManager<UmbMockPartialViewModel>(this);
|
||||
item = new UmbMockFileSystemItemManager<UmbMockPartialViewModel>(this);
|
||||
file;
|
||||
|
||||
constructor(data: Array<UmbMockPartialViewModel>) {
|
||||
super(data);
|
||||
|
||||
this.file = new UmbMockFileSystemDetailManager<UmbMockPartialViewModel>(this, {
|
||||
createMapper: this.#createPartialViewMockItemMapper,
|
||||
readMapper: this.#readPartialResponseMapper,
|
||||
});
|
||||
}
|
||||
|
||||
#createPartialViewMockItemMapper = (item: CreatePartialViewRequestModel): UmbMockPartialViewModel => {
|
||||
return {
|
||||
name: item.name,
|
||||
content: item.content,
|
||||
path: item.path,
|
||||
parentPath: item.parentPath,
|
||||
isFolder: false,
|
||||
hasChildren: false,
|
||||
};
|
||||
};
|
||||
|
||||
#readPartialResponseMapper = (item: UmbMockPartialViewModel): PartialViewResponseModel => {
|
||||
return {
|
||||
path: item.path,
|
||||
name: item.name,
|
||||
content: item.content,
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export const umbPartialViewMockDB = new UmbPartialViewMockDB(data);
|
||||
@@ -1,60 +1,83 @@
|
||||
const { rest } = window.MockServiceWorker;
|
||||
import { RestHandler, MockedRequest, DefaultBodyType } from 'msw';
|
||||
import { umbPartialViewsData, umbPartialViewsTreeData } from '../data/partial-views.data.js';
|
||||
import { umbPartialViewMockDB } from '../data/partial-view/partial-view.db.js';
|
||||
import { umbracoPath } from '@umbraco-cms/backoffice/utils';
|
||||
import { CreatePartialViewRequestModel, UpdatePartialViewRequestModel } from '@umbraco-cms/backoffice/backend-api';
|
||||
|
||||
const treeHandlers = [
|
||||
rest.get(umbracoPath('/tree/partial-view/root'), (req, res, ctx) => {
|
||||
const response = umbPartialViewsTreeData.getTreeRoot();
|
||||
const response = umbPartialViewMockDB.tree.getRoot();
|
||||
return res(ctx.status(200), ctx.json(response));
|
||||
}),
|
||||
|
||||
rest.get(umbracoPath('/tree/partial-view/children'), (req, res, ctx) => {
|
||||
const path = req.url.searchParams.get('path');
|
||||
if (!path) return;
|
||||
|
||||
const response = umbPartialViewsTreeData.getTreeItemChildren(path);
|
||||
if (!path) return res(ctx.status(400));
|
||||
const response = umbPartialViewMockDB.tree.getChildrenOf(path);
|
||||
return res(ctx.status(200), ctx.json(response));
|
||||
}),
|
||||
|
||||
rest.get(umbracoPath('/tree/partial-view/item'), (req, res, ctx) => {
|
||||
const paths = req.url.searchParams.getAll('paths');
|
||||
if (!paths) return;
|
||||
|
||||
const items = umbPartialViewsTreeData.getTreeItem(paths);
|
||||
return res(ctx.status(200), ctx.json(items));
|
||||
}),
|
||||
];
|
||||
|
||||
const detailHandlers: RestHandler<MockedRequest<DefaultBodyType>>[] = [
|
||||
rest.get(umbracoPath('/partial-view'), (req, res, ctx) => {
|
||||
const path = decodeURIComponent(req.url.searchParams.get('path') ?? '').replace('-cshtml', '.cshtml');
|
||||
if (!path) return res(ctx.status(400));
|
||||
const response = umbPartialViewsData.getPartialView(path);
|
||||
return res(ctx.status(200), ctx.json(response));
|
||||
}),
|
||||
|
||||
rest.post(umbracoPath('/partial-view'), async (req, res, ctx) => {
|
||||
const requestBody = await req.json();
|
||||
if (!requestBody) return res(ctx.status(400, 'no body found'));
|
||||
const response = umbPartialViewsData.insertPartialView(requestBody);
|
||||
return res(ctx.status(200), ctx.json(response));
|
||||
}),
|
||||
|
||||
rest.delete(umbracoPath('/partial-view'), (req, res, ctx) => {
|
||||
rest.get(umbracoPath('/script'), (req, res, ctx) => {
|
||||
const path = req.url.searchParams.get('path');
|
||||
if (!path) return res(ctx.status(400));
|
||||
umbPartialViewsData.delete([path]);
|
||||
const response = umbPartialViewMockDB.file.read(path);
|
||||
return res(ctx.status(200), ctx.json(response));
|
||||
}),
|
||||
|
||||
rest.post(umbracoPath('/script'), async (req, res, ctx) => {
|
||||
const requestBody = (await req.json()) as CreatePartialViewRequestModel;
|
||||
if (!requestBody) return res(ctx.status(400, 'no body found'));
|
||||
umbPartialViewMockDB.file.create(requestBody);
|
||||
return res(ctx.status(200));
|
||||
}),
|
||||
|
||||
rest.put(umbracoPath('/partial-view'), async (req, res, ctx) => {
|
||||
const requestBody = await req.json();
|
||||
rest.delete(umbracoPath('/script'), (req, res, ctx) => {
|
||||
const path = req.url.searchParams.get('path');
|
||||
if (!path) return res(ctx.status(400));
|
||||
umbPartialViewMockDB.file.delete(path);
|
||||
return res(ctx.status(200));
|
||||
}),
|
||||
|
||||
rest.put(umbracoPath('/script'), async (req, res, ctx) => {
|
||||
const requestBody = (await req.json()) as UpdatePartialViewRequestModel;
|
||||
if (!requestBody) return res(ctx.status(400, 'no body found'));
|
||||
umbPartialViewsData.updateData(requestBody);
|
||||
umbPartialViewMockDB.file.update(requestBody);
|
||||
return res(ctx.status(200));
|
||||
}),
|
||||
];
|
||||
|
||||
const itemHandlers: RestHandler<MockedRequest<DefaultBodyType>>[] = [
|
||||
rest.get(umbracoPath('/script/item'), (req, res, ctx) => {
|
||||
const paths = req.url.searchParams.getAll('paths');
|
||||
if (!paths) return res(ctx.status(400, 'no body found'));
|
||||
const response = umbPartialViewMockDB.item.getItems(paths);
|
||||
return res(ctx.status(200), ctx.json(response));
|
||||
}),
|
||||
];
|
||||
|
||||
const folderHandlers: RestHandler<MockedRequest<DefaultBodyType>>[] = [
|
||||
rest.get(umbracoPath('/script/folder'), (req, res, ctx) => {
|
||||
const path = req.url.searchParams.get('path');
|
||||
if (!path) return res(ctx.status(400));
|
||||
const response = umbPartialViewMockDB.folder.read(path);
|
||||
return res(ctx.status(200), ctx.json(response));
|
||||
}),
|
||||
|
||||
rest.post(umbracoPath('/script/folder'), async (req, res, ctx) => {
|
||||
const requestBody = (await req.json()) as CreatePartialViewRequestModel;
|
||||
if (!requestBody) return res(ctx.status(400, 'no body found'));
|
||||
umbPartialViewMockDB.folder.create(requestBody);
|
||||
return res(ctx.status(200));
|
||||
}),
|
||||
|
||||
rest.delete(umbracoPath('/script/folder'), (req, res, ctx) => {
|
||||
const path = req.url.searchParams.get('path');
|
||||
if (!path) return res(ctx.status(400));
|
||||
umbPartialViewMockDB.folder.delete(path);
|
||||
return res(ctx.status(200));
|
||||
}),
|
||||
];
|
||||
const folderHandlers: RestHandler<MockedRequest<DefaultBodyType>>[] = [];
|
||||
|
||||
export const handlers = [...treeHandlers, ...detailHandlers, ...folderHandlers];
|
||||
|
||||
Reference in New Issue
Block a user