Skip to content

Commit

Permalink
fix: Different types for request and response headers
Browse files Browse the repository at this point in the history
Improves Deno support where Response.headers is always Record<string, string>
  • Loading branch information
enisdenjo committed Aug 5, 2022
1 parent 398f68e commit 9e91141
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
11 changes: 10 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ graphql-http
- [Client](interfaces/Client.md)
- [ClientOptions](interfaces/ClientOptions.md)
- [HandlerOptions](interfaces/HandlerOptions.md)
- [Headers](interfaces/Headers.md)
- [Request](interfaces/Request.md)
- [RequestHeaders](interfaces/RequestHeaders.md)
- [RequestParams](interfaces/RequestParams.md)
- [ResponseInit](interfaces/ResponseInit.md)
- [Sink](interfaces/Sink.md)
Expand All @@ -25,6 +25,7 @@ graphql-http
- [Handler](README.md#handler)
- [Response](README.md#response)
- [ResponseBody](README.md#responsebody)
- [ResponseHeaders](README.md#responseheaders)

### Functions

Expand Down Expand Up @@ -71,6 +72,14 @@ to be coerced to the server implementation in use.

___

### ResponseHeaders

Ƭ **ResponseHeaders**: { `accept?`: `string` ; `allow?`: `string` ; `content-type?`: `string` } & `Record`<`string`, `string`\>

The response headers that get returned from graphql-http.

___

### isResponse

**isResponse**(`val`): val is Response
Expand Down
2 changes: 1 addition & 1 deletion docs/interfaces/Request.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ ___

### headers

`Readonly` **headers**: [`Headers`](Headers.md)
`Readonly` **headers**: [`RequestHeaders`](RequestHeaders.md)

___

Expand Down
14 changes: 7 additions & 7 deletions docs/interfaces/Headers.md → docs/interfaces/RequestHeaders.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[graphql-http](../README.md) / Headers
[graphql-http](../README.md) / RequestHeaders

# Interface: Headers
# Interface: RequestHeaders

Concrete interface that the headers map should implement.
The incoming request headers the implementing server should provide.

## Indexable

Expand All @@ -12,10 +12,10 @@ Concrete interface that the headers map should implement.

### Properties

- [accept](Headers.md#accept)
- [allow](Headers.md#allow)
- [content-type](Headers.md#content-type)
- [set-cookie](Headers.md#set-cookie)
- [accept](RequestHeaders.md#accept)
- [allow](RequestHeaders.md#allow)
- [content-type](RequestHeaders.md#content-type)
- [set-cookie](RequestHeaders.md#set-cookie)

## Properties

Expand Down
2 changes: 1 addition & 1 deletion docs/interfaces/ResponseInit.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Server agnostic response options (ex. status and headers) returned from

### headers

`Optional` `Readonly` **headers**: [`Headers`](Headers.md)
`Optional` `Readonly` **headers**: [`ResponseHeaders`](../README.md#responseheaders)

___

Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fetch from 'node-fetch';
import { Headers } from '../common';
import { RequestHeaders } from '../common';
import { createClient, NetworkError } from '../client';
import { startTServer } from './utils/tserver';
import { texecute } from './utils/texecute';

it('should use the provided headers', async () => {
let headers: Headers = {};
let headers: RequestHeaders = {};
const server = startTServer({
onSubscribe: (req) => {
headers = req.headers;
Expand Down
19 changes: 15 additions & 4 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import { isObject } from './utils';

/**
* Concrete interface that the headers map should implement.
* The incoming request headers the implementing server should provide.
*
* @category Common
*/
export interface Headers {
export interface RequestHeaders {
accept?: string | undefined;
allow?: string | undefined;
'content-type'?: string | undefined;
Expand All @@ -33,7 +33,7 @@ export interface Headers {
export interface Request<RawRequest> {
readonly method: string;
readonly url: string;
readonly headers: Headers;
readonly headers: RequestHeaders;
readonly body: string | Record<string, unknown> | null;
/**
* The raw request itself from the implementing server.
Expand All @@ -58,6 +58,17 @@ export interface RequestParams {
extensions?: Record<string, unknown> | undefined;
}

/**
* The response headers that get returned from graphql-http.
*
* @category Common
*/
export type ResponseHeaders = {
accept?: string;
allow?: string;
'content-type'?: string;
} & Record<string, string>;

/**
* Server agnostic response body returned from `graphql-http` needing
* to be coerced to the server implementation in use.
Expand All @@ -75,7 +86,7 @@ export type ResponseBody = string;
export interface ResponseInit {
readonly status: number;
readonly statusText?: string;
readonly headers?: Headers;
readonly headers?: ResponseHeaders;
}

/**
Expand Down

0 comments on commit 9e91141

Please sign in to comment.