@appsemble/node-utils
Version:
NodeJS utilities used by Appsemble internally.
92 lines (91 loc) • 5.22 kB
TypeScript
import { type AppDefinition, type ResourceDefinition } from '@appsemble/lang-sdk';
import { type App, type Resource as ResourceType } from '@appsemble/types';
import { type PreValidatePropertyFunction } from 'jsonschema';
import { type Context, type DefaultContext, type DefaultState, type ParameterizedContext } from 'koa';
import { type JsonValue } from 'type-fest';
import { type GetAppResourcesParams, type PreparedAsset, TempFile } from './index.js';
export declare function stripResource({ $author, $created, $editor, $ephemeral, $etag, $group, $seed, $updated, ...data }: ResourceType): Record<string, unknown>;
/**
* Works on a resource, which has been processed on the server by the streamParser
*
* @param data The resource to be serialized, optionally containing parsed TempFile instance assets
* @returns An object containing the resource and an array of the assets referenced
* from the resource
*/
export declare function serializeServerResource(data: any): JsonValue | {
resource: JsonValue;
assets: TempFile[];
};
export type SerializedServerResourceBody = ReturnType<typeof serializeServerResource>;
export type SerializedMultipartBody = Extract<SerializedServerResourceBody, {
resource: JsonValue;
assets: TempFile[];
}>;
export declare function isSerializedMultipartBody(value: SerializedServerResourceBody): value is SerializedMultipartBody;
/**
* Get the resource definition of an app by name.
*
* If there is no match, a 404 HTTP error is thrown.
*
* @param appDefinition The app definition to get the resource definition of
* @param resourceType The name of the resource definition to get.
* @param ctx Context used to throw back the errors.
* @param view The view that’s being used.
* @returns The matching resource definition.
*/
export declare function getResourceDefinition(appDefinition: AppDefinition, resourceType: string, ctx?: Context, view?: string): ResourceDefinition;
/**
* Extracts the IDs of resource request body.
*
* @param ctx The Koa context to extract the body from.
* @param suppliedBody A resource body to use directly without reading from context.
* Supports multipart-style `{ resource, assets }` input.
* @returns A tuple which consists of:
*
* 1. One or more resources processed from the request body.
* 2. A list of newly uploaded assets which should be linked to the resources.
* 3. preValidateProperty function used for reconstructing resources from a CSV file.
*/
export declare function extractResourceBody(ctx: Context | ParameterizedContext<DefaultState, DefaultContext, any> | undefined, suppliedBody?: SerializedServerResourceBody): [
Record<string, unknown> | Record<string, unknown>[],
TempFile[],
PreValidatePropertyFunction | undefined
];
/**
* Process an incoming resource request body.
*
* This handles JSON schema validation, resource expiration, and asset linking and validation.
*
* @param ctx The Koa context to process.
* @param definition The resource definition to use for processing the request body.
* @param knownAssetIds A list of asset IDs that are already known to be linked to the resource.
* @param knownExpires A previously known expires value.
* @param knownAssetNameIds A list of asset ids with asset names that already exist.
* @param [isPatch] if the "PATCH" HTTP method is being used.
* @param resourceBody Used to process resources from non-request contexts.
* @returns A tuple which consists of:
*
* 1. One or more resources processed from the request body.
* 2. A list of newly uploaded assets which should be linked to the resources.
* 3. Asset IDs from the `knownAssetIds` array which are no longer used.
*/
export declare function processResourceBody(ctx: Context | ParameterizedContext<DefaultState, DefaultContext, any>, definition: ResourceDefinition, knownAssetIds?: string[], knownExpires?: Date, knownAssetNameIds?: {
id: string;
name?: string;
}[], isPatch?: boolean, resourceBody?: SerializedServerResourceBody): Promise<[Record<string, unknown> | Record<string, unknown>[], PreparedAsset[], string[]]>;
/**
* Validate that the references declared in the resource definition point at existing resources.
*
* Reference properties which hold no value are skipped. Expired resources are treated as
* non-existent. The referenced ids are collected per referenced resource type, so each type is
* checked using a single lookup, even when multiple resources are submitted at once.
*
* If a reference is broken, an HTTP 400 error naming the offending property path is thrown.
*
* @param ctx The Koa context used to throw back the error response.
* @param app The app the resources belong to.
* @param definition The definition of the resource type being created or updated.
* @param processedBody One or more resources as processed by `processResourceBody`.
* @param getAppResources The function used to look up resources of the referenced types.
*/
export declare function validateResourceReferences(ctx: Context, app: App, definition: ResourceDefinition, processedBody: Record<string, unknown> | Record<string, unknown>[], getAppResources: (params: GetAppResourcesParams) => Promise<ResourceType[]>): Promise<void>;