UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

38 lines (37 loc) 1.63 kB
import type { Validator } from "../util/validate.js"; /** * An abstract API resource definition, used to specify types for e.g. serverless functions.. * * @param name The name of the resource, e.g. `getUser`. * @param payload The `Validator` the resource's payload must conform to (defaults to `undefined` if not specified). * @param returns The `Validator` the resource's returned value must conform to (defaults to `undefined` if not specified). */ export declare class Resource<P = undefined, R = void> implements Validator<R> { /** Resource name.. */ readonly name: string; /** Payload validator. */ readonly payload: Validator<P>; /** Result validator. */ readonly result: Validator<R>; constructor(name: string, payload: Validator<P>, result: Validator<R>); constructor(name: string, payload: Validator<P>); constructor(name: string); /** * Validate a payload for this resource. * * @returns The validated payload for this resource. * @throws Feedback if the payload could not be validated. */ prepare(unsafePayload: unknown): P; /** * Validate a result for this resource. * * @returns The validated result for this resource. * @throws ValueError if the result could not be validated. */ validate(unsafeResult: unknown): R; } /** Extract the payload type from a `Resource`. */ export type PayloadType<X extends Resource> = X extends Resource<infer Y, unknown> ? Y : never; /** Extract the result type from a `Resource`. */ export type ResourceType<X extends Resource> = X extends Resource<unknown, infer Y> ? Y : never;