Skip to content

Commit

Permalink
feat(use): Each adapter has an adapted HandlerOptions interface
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Feb 13, 2023
1 parent 01f278c commit f14a821
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 22 deletions.
20 changes: 19 additions & 1 deletion docs/modules/use_express.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,30 @@

## Table of contents

### Type Aliases

- [HandlerOptions](use_express.md#handleroptions)

### Functions

- [createHandler](use_express.md#createhandler)

## Server/express

### HandlerOptions

Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`Request`, `undefined`, `Context`\>

Handler options when using the express adapter.

#### Type parameters

| Name | Type |
| :------ | :------ |
| `Context` | extends [`OperationContext`](handler.md#operationcontext) = `undefined` |

___

### createHandler

**createHandler**<`Context`\>(`options`): `Handler`
Expand Down Expand Up @@ -39,7 +57,7 @@ console.log('Listening to port 4000');

| Name | Type |
| :------ | :------ |
| `options` | [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`Request`<`ParamsDictionary`, `any`, `any`, `ParsedQs`, `Record`<`string`, `any`\>\>, `undefined`, `Context`\> |
| `options` | [`HandlerOptions`](use_express.md#handleroptions)<`Context`\> |

#### Returns

Expand Down
20 changes: 19 additions & 1 deletion docs/modules/use_fastify.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,30 @@

## Table of contents

### Type Aliases

- [HandlerOptions](use_fastify.md#handleroptions)

### Functions

- [createHandler](use_fastify.md#createhandler)

## Server/fastify

### HandlerOptions

Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`FastifyRequest`, `undefined`, `Context`\>

Handler options when using the fastify adapter.

#### Type parameters

| Name | Type |
| :------ | :------ |
| `Context` | extends [`OperationContext`](handler.md#operationcontext) = `undefined` |

___

### createHandler

**createHandler**<`Context`\>(`options`): `RouteHandler`
Expand Down Expand Up @@ -39,7 +57,7 @@ console.log('Listening to port 4000');

| Name | Type |
| :------ | :------ |
| `options` | [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`FastifyRequest`<`RouteGenericInterface`, `RawServerDefault`, `IncomingMessage`, `FastifySchema`, `FastifyTypeProviderDefault`, `unknown`, `FastifyBaseLogger`, `ResolveFastifyRequestType`<`FastifyTypeProviderDefault`, `FastifySchema`, `RouteGenericInterface`\>\>, `undefined`, `Context`\> |
| `options` | [`HandlerOptions`](use_fastify.md#handleroptions)<`Context`\> |

#### Returns

Expand Down
24 changes: 21 additions & 3 deletions docs/modules/use_fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,33 @@

- [FetchAPI](../interfaces/use_fetch.FetchAPI.md)

### Type Aliases

- [HandlerOptions](use_fetch.md#handleroptions)

### Functions

- [createHandler](use_fetch.md#createhandler)

## Server/fetch

### HandlerOptions

Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`Request`, [`FetchAPI`](../interfaces/use_fetch.FetchAPI.md), `Context`\>

Handler options when using the fetch adapter.

#### Type parameters

| Name | Type |
| :------ | :------ |
| `Context` | extends [`OperationContext`](handler.md#operationcontext) = `undefined` |

___

### createHandler

**createHandler**<`Context`\>(`options`, `fetchApi?`): (`req`: `Request`) => `Promise`<`Response`\>
**createHandler**<`Context`\>(`options`, `reqCtx?`): (`req`: `Request`) => `Promise`<`Response`\>

Create a GraphQL over HTTP Protocol compliant request handler for
a fetch environment like Deno, Bun, CloudFlare Workers, Lambdas, etc.
Expand Down Expand Up @@ -51,8 +69,8 @@ console.log('Listening to port 4000');

| Name | Type | Description |
| :------ | :------ | :------ |
| `options` | [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`Request`, [`FetchAPI`](../interfaces/use_fetch.FetchAPI.md), `Context`\> | - |
| `fetchApi` | `Partial`<[`FetchAPI`](../interfaces/use_fetch.FetchAPI.md)\> | Custom fetch API engine, will use from global scope if left undefined. |
| `options` | [`HandlerOptions`](use_fetch.md#handleroptions)<`Context`\> | - |
| `reqCtx` | `Partial`<[`FetchAPI`](../interfaces/use_fetch.FetchAPI.md)\> | Custom fetch API engine, will use from global scope if left undefined. |

#### Returns

Expand Down
20 changes: 19 additions & 1 deletion docs/modules/use_koa.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,30 @@

## Table of contents

### Type Aliases

- [HandlerOptions](use_koa.md#handleroptions)

### Functions

- [createHandler](use_koa.md#createhandler)

## Server/koa

### HandlerOptions

Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`IncomingMessage`, `undefined`, `Context`\>

Handler options when using the koa adapter.

#### Type parameters

| Name | Type |
| :------ | :------ |
| `Context` | extends [`OperationContext`](handler.md#operationcontext) = `undefined` |

___

### createHandler

**createHandler**<`Context`\>(`options`): `Middleware`
Expand Down Expand Up @@ -40,7 +58,7 @@ console.log('Listening to port 4000');

| Name | Type |
| :------ | :------ |
| `options` | [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`IncomingMessage`, `undefined`, `Context`\> |
| `options` | [`HandlerOptions`](use_koa.md#handleroptions)<`Context`\> |

#### Returns

Expand Down
20 changes: 19 additions & 1 deletion docs/modules/use_node.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,30 @@

## Table of contents

### Type Aliases

- [HandlerOptions](use_node.md#handleroptions)

### Functions

- [createHandler](use_node.md#createhandler)

## Server/node

### HandlerOptions

Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`IncomingMessage`, `undefined`, `Context`\>

Handler options when using the node adapter.

#### Type parameters

| Name | Type |
| :------ | :------ |
| `Context` | extends [`OperationContext`](handler.md#operationcontext) = `undefined` |

___

### createHandler

**createHandler**<`Context`\>(`options`): `RequestListener`
Expand Down Expand Up @@ -38,7 +56,7 @@ console.log('Listening to port 4000');

| Name | Type |
| :------ | :------ |
| `options` | [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`IncomingMessage`, `undefined`, `Context`\> |
| `options` | [`HandlerOptions`](use_node.md#handleroptions)<`Context`\> |

#### Returns

Expand Down
12 changes: 10 additions & 2 deletions src/use/express.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import type { Request, Handler } from 'express';
import {
createHandler as createRawHandler,
HandlerOptions,
HandlerOptions as RawHandlerOptions,
OperationContext,
} from '../handler';

/**
* Handler options when using the express adapter.
*
* @category Server/express
*/
export type HandlerOptions<Context extends OperationContext = undefined> =
RawHandlerOptions<Request, undefined, Context>;

/**
* Create a GraphQL over HTTP Protocol compliant request handler for
* the express framework.
Expand All @@ -24,7 +32,7 @@ import {
* @category Server/express
*/
export function createHandler<Context extends OperationContext = undefined>(
options: HandlerOptions<Request, undefined, Context>,
options: HandlerOptions<Context>,
): Handler {
const isProd = process.env.NODE_ENV === 'production';
const handle = createRawHandler(options);
Expand Down
12 changes: 10 additions & 2 deletions src/use/fastify.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import type { FastifyRequest, RouteHandler } from 'fastify';
import {
createHandler as createRawHandler,
HandlerOptions,
HandlerOptions as RawHandlerOptions,
OperationContext,
} from '../handler';

/**
* Handler options when using the fastify adapter.
*
* @category Server/fastify
*/
export type HandlerOptions<Context extends OperationContext = undefined> =
RawHandlerOptions<FastifyRequest, undefined, Context>;

/**
* Create a GraphQL over HTTP Protocol compliant request handler for
* the fastify framework.
Expand All @@ -24,7 +32,7 @@ import {
* @category Server/fastify
*/
export function createHandler<Context extends OperationContext = undefined>(
options: HandlerOptions<FastifyRequest, undefined, Context>,
options: HandlerOptions<Context>,
): RouteHandler {
const isProd = process.env.NODE_ENV === 'production';
const handle = createRawHandler(options);
Expand Down
22 changes: 15 additions & 7 deletions src/use/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
createHandler as createRawHandler,
HandlerOptions,
HandlerOptions as RawHandlerOptions,
OperationContext,
} from '../handler';

Expand All @@ -15,6 +15,14 @@ export interface FetchAPI {
TextEncoder: typeof TextEncoder;
}

/**
* Handler options when using the fetch adapter.
*
* @category Server/fetch
*/
export type HandlerOptions<Context extends OperationContext = undefined> =
RawHandlerOptions<Request, FetchAPI, Context>;

/**
* Create a GraphQL over HTTP Protocol compliant request handler for
* a fetch environment like Deno, Bun, CloudFlare Workers, Lambdas, etc.
Expand All @@ -39,19 +47,19 @@ export interface FetchAPI {
* console.log('Listening to port 4000');
* ```
*
* @param fetchApi - Custom fetch API engine, will use from global scope if left undefined.
* @param reqCtx - Custom fetch API engine, will use from global scope if left undefined.
*
* @category Server/fetch
*/
export function createHandler<Context extends OperationContext = undefined>(
options: HandlerOptions<Request, FetchAPI, Context>,
fetchApi: Partial<FetchAPI> = {},
options: HandlerOptions<Context>,
reqCtx: Partial<FetchAPI> = {},
): (req: Request) => Promise<Response> {
const isProd = process.env.NODE_ENV === 'production';
const api: FetchAPI = {
Response: fetchApi.Response || Response,
TextEncoder: fetchApi.TextEncoder || TextEncoder,
ReadableStream: fetchApi.ReadableStream || ReadableStream,
Response: reqCtx.Response || Response,
TextEncoder: reqCtx.TextEncoder || TextEncoder,
ReadableStream: reqCtx.ReadableStream || ReadableStream,
};
const handler = createRawHandler(options);
return async function handleRequest(req) {
Expand Down
12 changes: 10 additions & 2 deletions src/use/koa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ import type { Middleware } from 'koa';
import type { IncomingMessage } from 'http';
import {
createHandler as createRawHandler,
HandlerOptions,
HandlerOptions as RawHandlerOptions,
OperationContext,
} from '../handler';

/**
* Handler options when using the koa adapter.
*
* @category Server/koa
*/
export type HandlerOptions<Context extends OperationContext = undefined> =
RawHandlerOptions<IncomingMessage, undefined, Context>;

/**
* Create a GraphQL over HTTP Protocol compliant request handler for
* the Koa framework.
Expand All @@ -26,7 +34,7 @@ import {
* @category Server/koa
*/
export function createHandler<Context extends OperationContext = undefined>(
options: HandlerOptions<IncomingMessage, undefined, Context>,
options: HandlerOptions<Context>,
): Middleware {
const isProd = process.env.NODE_ENV === 'production';
const handle = createRawHandler(options);
Expand Down
12 changes: 10 additions & 2 deletions src/use/node.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import type { IncomingMessage, RequestListener } from 'http';
import {
createHandler as createRawHandler,
HandlerOptions,
HandlerOptions as RawHandlerOptions,
OperationContext,
} from '../handler';

/**
* Handler options when using the node adapter.
*
* @category Server/node
*/
export type HandlerOptions<Context extends OperationContext = undefined> =
RawHandlerOptions<IncomingMessage, undefined, Context>;

/**
* Create a GraphQL over HTTP Protocol compliant request handler for
* the Node environment.
Expand All @@ -23,7 +31,7 @@ import {
* @category Server/node
*/
export function createHandler<Context extends OperationContext = undefined>(
options: HandlerOptions<IncomingMessage, undefined, Context>,
options: HandlerOptions<Context>,
): RequestListener {
const isProd = process.env.NODE_ENV === 'production';
const handle = createRawHandler(options);
Expand Down

0 comments on commit f14a821

Please sign in to comment.