fixed document detail data source
This commit is contained in:
@@ -3,7 +3,7 @@ import type { DataSourceResponse } from '@umbraco-cms/models';
|
||||
export interface RepositoryDetailDataSource<DetailType> {
|
||||
createScaffold(parentKey: string | null): Promise<DataSourceResponse<DetailType>>;
|
||||
get(key: string): Promise<DataSourceResponse<DetailType>>;
|
||||
insert(data: DetailType): Promise<DataSourceResponse>;
|
||||
update(data: DetailType): Promise<DataSourceResponse>;
|
||||
trash(key: string): Promise<DataSourceResponse>;
|
||||
insert(data: DetailType): Promise<DataSourceResponse<DetailType>>;
|
||||
update(data: DetailType): Promise<DataSourceResponse<DetailType>>;
|
||||
trash(key: string): Promise<DataSourceResponse<DetailType>>;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
import { ApiError, CancelablePromise, ProblemDetails } from '@umbraco-cms/backend-api';
|
||||
import { UmbController, UmbControllerHostInterface } from '@umbraco-cms/controller';
|
||||
import { UmbContextConsumerController } from '@umbraco-cms/context-api';
|
||||
import type { DataSourceResponse } from '@umbraco-cms/models';
|
||||
|
||||
export class UmbResourceController extends UmbController {
|
||||
#promise: Promise<any>;
|
||||
@@ -54,7 +55,7 @@ export class UmbResourceController extends UmbController {
|
||||
/**
|
||||
* Base execute function with a try/catch block and return a tuple with the result and the error.
|
||||
*/
|
||||
static async tryExecute<T>(promise: Promise<T>): Promise<{ data?: T; error?: ProblemDetails }> {
|
||||
static async tryExecute<T>(promise: Promise<T>): Promise<DataSourceResponse<T>> {
|
||||
try {
|
||||
return { data: await promise };
|
||||
} catch (e) {
|
||||
@@ -66,7 +67,7 @@ export class UmbResourceController extends UmbController {
|
||||
* Wrap the {execute} function in a try/catch block and return the result.
|
||||
* If the executor function throws an error, then show the details in a notification.
|
||||
*/
|
||||
async tryExecuteAndNotify<T>(options?: UmbNotificationOptions<any>): Promise<{ data?: T; error?: ProblemDetails }> {
|
||||
async tryExecuteAndNotify<T>(options?: UmbNotificationOptions<any>): Promise<DataSourceResponse<T>> {
|
||||
const { data, error } = await UmbResourceController.tryExecute<T>(this.#promise);
|
||||
|
||||
if (error) {
|
||||
|
||||
@@ -2,7 +2,7 @@ import { RepositoryDetailDataSource } from '@umbraco-cms/repository';
|
||||
import { ProblemDetails } from '@umbraco-cms/backend-api';
|
||||
import { UmbControllerHostInterface } from '@umbraco-cms/controller';
|
||||
import { tryExecuteAndNotify } from '@umbraco-cms/resources';
|
||||
import type { DocumentDetails } from '@umbraco-cms/models';
|
||||
import type { DataSourceResponse, DocumentDetails } from '@umbraco-cms/models';
|
||||
|
||||
/**
|
||||
* A data source for the Template detail that fetches data from the server
|
||||
@@ -94,10 +94,10 @@ export class UmbDocumentDetailServerDataSource implements RepositoryDetailDataSo
|
||||
* @return {*}
|
||||
* @memberof UmbDocumentDetailServerDataSource
|
||||
*/
|
||||
insert(document: DocumentDetails) {
|
||||
async insert(document: DocumentDetails) {
|
||||
if (!document.key) {
|
||||
const error: ProblemDetails = { title: 'Document key is missing' };
|
||||
return { error };
|
||||
//const error: ProblemDetails = { title: 'Document key is missing' };
|
||||
return Promise.reject();
|
||||
}
|
||||
//const payload = { key: document.key, requestBody: document };
|
||||
|
||||
@@ -110,7 +110,7 @@ export class UmbDocumentDetailServerDataSource implements RepositoryDetailDataSo
|
||||
return Promise.reject();
|
||||
}
|
||||
//return tryExecuteAndNotify(this.#host, DocumentResource.postDocument(payload));
|
||||
return tryExecuteAndNotify(this.#host,
|
||||
return tryExecuteAndNotify<DocumentDetails>(this.#host,
|
||||
fetch('/umbraco/management/api/v1/document/save', {
|
||||
method: 'POST',
|
||||
body: body,
|
||||
@@ -118,7 +118,7 @@ export class UmbDocumentDetailServerDataSource implements RepositoryDetailDataSo
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}
|
||||
)
|
||||
) as any
|
||||
);
|
||||
}
|
||||
|
||||
@@ -145,7 +145,7 @@ export class UmbDocumentDetailServerDataSource implements RepositoryDetailDataSo
|
||||
return { error: myError };
|
||||
}
|
||||
|
||||
return tryExecuteAndNotify(this.#host,
|
||||
return tryExecuteAndNotify<DocumentDetails>(this.#host,
|
||||
fetch('/umbraco/management/api/v1/document/save', {
|
||||
method: 'POST',
|
||||
body: body,
|
||||
@@ -153,9 +153,31 @@ export class UmbDocumentDetailServerDataSource implements RepositoryDetailDataSo
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}
|
||||
)
|
||||
) as any
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trash a Document on the server
|
||||
* @param {Document} Document
|
||||
* @return {*}
|
||||
* @memberof UmbDocumentDetailServerDataSource
|
||||
*/
|
||||
async trash(key: string) {
|
||||
if (!key) {
|
||||
const error: ProblemDetails = { title: 'Key is missing' };
|
||||
return { error };
|
||||
}
|
||||
|
||||
return tryExecuteAndNotify<DocumentDetails>(this.#host,
|
||||
fetch('/umbraco/management/api/v1/document/trash', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify([key]),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}) as any
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user