@h4ad/serverless-adapter
Version:
Run REST APIs and other web applications using your existing Node.js application framework (NestJS, Express, Koa, Hapi, Fastify and many others), on top of AWS, Azure, Digital Ocean and many other clouds.
155 lines (152 loc) • 5.91 kB
TypeScript
import { IncomingMessage, ServerResponse } from 'http';
import { DataTransformer, AnyRouter } from '@trpc/server';
import { NodeHTTPHandlerOptions, NodeHTTPCreateContextFnOptions } from '@trpc/server/adapters/node-http';
import { S as SingleValueHeaders } from '../../headers-DjfGHDmI.js';
import { F as FrameworkContract } from '../../framework.contract-td-lRvq6.js';
/**
* The transformer that is responsible to transform buffer's input to javascript objects
*
* @deprecated You should use {@link JsonBodyParserFramework} instead, is more reliable and enable you to use transformer of trpc to other things.
* @breadcrumb Frameworks / TrpcFramework
* @public
*/
declare class BufferToJSObjectTransformer implements DataTransformer {
/**
* Deserialize unknown values to javascript objects
*
* @param value - The value to be deserialized
*/
deserialize(value?: unknown): any;
/**
* The value to be serialized, do nothing because tRPC can handle.
*
* @param value - The value to be serialized
*/
serialize(value: any): any;
}
/**
* The context created by this library that allows getting some information from the request and setting the status and header of the response.
*
* @breadcrumb Frameworks / TrpcFramework
* @public
*/
interface TrpcAdapterBaseContext {
/**
* The request object that will be forward to your app
*/
request: IncomingMessage;
/**
* The response object that will be forward to your app to output the response
*/
response: ServerResponse;
/**
* The method to set response status.
*
* @param statusCode - The response status that you want
*/
setStatus(statusCode: number): void;
/**
* The method to set some header in the response
*
* @param name - The name of the header
* @param value - The value to be set in the header
*/
setHeader(name: string, value: number | string): void;
/**
* The method to remove some header from the response
*
* @param name - The name of the header
*/
removeHeader(name: string): void;
/**
* The method to return the value of some header from the request
*
* @param name - The name of the header
*/
getHeader(name: string): string | undefined;
/**
* The method to return the request headers
*/
getHeaders(): SingleValueHeaders;
/**
* The method to return user's address
*/
getIp(): string | undefined;
/**
* The method to return the URL called
*/
getUrl(): string | undefined;
/**
* The method to return the HTTP Method in the request
*/
getMethod(): string | undefined;
}
/**
* This is the context merged between {@link TrpcAdapterBaseContext} and the {@link TContext} that you provided.
*
* This context will be merged with the context you created with `createContext` inside {@link TrpcFrameworkOptions}.
* So to make the type work, just send the properties you've added inside {@link TContext}.
*
* @example
* ```typescript
* type MyCustomContext = { user: { name: string } };
* type TrpcContext = TrpcAdapterContext<MyCustomContext>; // your final context type to put inside trpc.router
* ```
*
* @breadcrumb Frameworks / TrpcFramework
* @public
*/
type TrpcAdapterContext<TContext> = TContext & TrpcAdapterBaseContext;
/**
* The options to customize the {@link TrpcFramework}
*
* @breadcrumb Frameworks / TrpcFramework
* @public
*/
type TrpcFrameworkOptions<TContext> = Omit<NodeHTTPHandlerOptions<AnyRouter, IncomingMessage, ServerResponse>, 'router' | 'createContext'> & {
createContext?: (opts: NodeHTTPCreateContextFnOptions<IncomingMessage, ServerResponse>) => Omit<TContext, keyof TrpcAdapterBaseContext> | Promise<Omit<TContext, keyof TrpcAdapterBaseContext>>;
};
/**
* The framework that forwards requests to TRPC handler
*
* @breadcrumb Frameworks / TrpcFramework
* @public
*/
declare class TrpcFramework<TContext extends TrpcAdapterBaseContext, TRouter extends AnyRouter = AnyRouter> implements FrameworkContract<TRouter> {
protected readonly options?: TrpcFrameworkOptions<TContext> | undefined;
/**
* Default constructor
*/
constructor(options?: TrpcFrameworkOptions<TContext> | undefined);
/**
* {@inheritDoc}
*/
sendRequest<TRouter extends AnyRouter>(app: TRouter, request: IncomingMessage, response: ServerResponse): void;
/**
* Get safe url that can be used inside Trpc.
*
* @example
* ```typescript
* const url = getSafeUrlForTrpc('/users?input=hello');
* console.log(url); // users
* ```
*
* @param request - The request object that will be forward to your app
*/
protected getSafeUrlForTrpc(request: IncomingMessage): string;
/**
* Merge the default context ({@link TrpcAdapterContext}) with the context created by the user.
*
* @param createContextOptions - The options sent by trpc to create the context
*/
protected mergeDefaultContextWithOptionsContext(createContextOptions: NodeHTTPCreateContextFnOptions<IncomingMessage, ServerResponse>): TContext | Promise<TContext>;
/**
* Wraps the resolved context from the {@link TrpcFrameworkOptions} created with `createContext` and merge
* with the {@link TrpcAdapterContext} generated by the library.
*
* @param resolvedContext - The context created with `createContext` inside {@link TrpcFrameworkOptions}
* @param createContextOptions - The options sent by trpc to create the context
*/
protected wrapResolvedContextWithDefaultContext(resolvedContext: TContext, createContextOptions: NodeHTTPCreateContextFnOptions<IncomingMessage, ServerResponse>): TContext;
}
export { BufferToJSObjectTransformer, type TrpcAdapterBaseContext, type TrpcAdapterContext, TrpcFramework, type TrpcFrameworkOptions };