UNPKG

3.91 kBTypeScriptView Raw
1/// <reference types="node" />
2import { PassThrough, Stream } from 'stream';
3import type { IncomingMessage, ServerResponse } from 'http';
4/******************************************************************************/
5export interface CompatFastifyReply {
6 raw: ServerResponse;
7 status(statusCode: number): CompatFastifyReply;
8 headers(values: {
9 [key: string]: any;
10 }): CompatFastifyReply;
11 send(payload?: any): CompatFastifyReply;
12}
13export interface CompatFastifyRequest {
14 raw: IncomingMessage;
15 body: unknown;
16 readonly headers: {
17 [key: string]: unknown;
18 };
19}
20/******************************************************************************/
21/******************************************************************************/
22export interface CompatKoaContext {
23 [key: string]: any;
24 req: IncomingMessage;
25 res: ServerResponse;
26}
27/******************************************************************************/
28declare module 'http' {
29 interface IncomingMessage {
30 _koaCtx?: CompatKoaContext;
31 _fastifyRequest?: CompatFastifyRequest;
32 _body?: boolean;
33 body?: any;
34 originalUrl?: string;
35 }
36}
37declare type Headers = {
38 [header: string]: string;
39};
40/**
41 * The base class for PostGraphile responses; collects headers, status code and
42 * body, and then hands to the relevant adaptor at the correct time.
43 */
44export declare abstract class PostGraphileResponse {
45 private _headers;
46 private _body;
47 private _setHeaders;
48 statusCode: number;
49 private _setHeadersOnce;
50 setHeader(header: string, value: string): void;
51 /**
52 * Use `endWithStream` or `end`; not both.
53 */
54 endWithStream(): PassThrough;
55 /**
56 * Use `endWithStream` or `end`; not both
57 */
58 end(moreBody?: Buffer | string | null): void;
59 /**
60 * Returns the `res` object that the underlying HTTP server would have.
61 */
62 abstract getNodeServerRequest(): IncomingMessage;
63 abstract getNodeServerResponse(): ServerResponse;
64 abstract setHeaders(statusCode: number, headers: Headers): void;
65 abstract setBody(body: Stream | Buffer | string | undefined): void;
66}
67/**
68 * Suitable for Node's HTTP server, but also connect, express, restify and fastify v2.
69 */
70export declare class PostGraphileResponseNode extends PostGraphileResponse {
71 private _req;
72 private _res;
73 private _next;
74 constructor(req: IncomingMessage, res: ServerResponse, next: (e?: 'route' | Error) => void);
75 getNodeServerRequest(): IncomingMessage;
76 getNodeServerResponse(): ServerResponse;
77 getNextCallback(): (e?: Error | "route" | undefined) => void;
78 setHeaders(statusCode: number, headers: Headers): void;
79 setBody(body: Stream | Buffer | string | undefined): void;
80}
81export declare type CompatKoaNext = (error?: Error) => Promise<any>;
82/**
83 * Suitable for Koa.
84 */
85export declare class PostGraphileResponseKoa extends PostGraphileResponse {
86 private _ctx;
87 private _next;
88 constructor(ctx: CompatKoaContext, next: CompatKoaNext);
89 getNodeServerRequest(): IncomingMessage;
90 getNodeServerResponse(): ServerResponse;
91 getNextCallback(): CompatKoaNext;
92 setHeaders(statusCode: number, headers: Headers): void;
93 endWithStream(): PassThrough;
94 setBody(body: Stream | Buffer | string | undefined): void;
95}
96/**
97 * Suitable for Fastify v3 (use PostGraphileResponseNode and middleware
98 * approach for Fastify v2)
99 */
100export declare class PostGraphileResponseFastify3 extends PostGraphileResponse {
101 private _request;
102 private _reply;
103 constructor(request: CompatFastifyRequest, reply: CompatFastifyReply);
104 getNodeServerRequest(): IncomingMessage;
105 getNodeServerResponse(): ServerResponse;
106 setHeaders(statusCode: number, headers: Headers): void;
107 endWithStream(): PassThrough;
108 setBody(body: Stream | Buffer | string | undefined): void;
109}
110export {};