Skip to content

Commit

Permalink
fix(client): TypeScript generic for ensuring proper arguments when us…
Browse files Browse the repository at this point in the history
…ing "single connection mode"
  • Loading branch information
enisdenjo committed Apr 14, 2022
1 parent f9b1fcf commit be2ae7d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 39 deletions.
10 changes: 8 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ graphql-sse

### createClient

**createClient**(`options`): [`Client`](interfaces/Client.md)
**createClient**<`SingleConnection`\>(`options`): [`Client`](interfaces/Client.md)

Creates a disposable GraphQL over SSE client to transmit
GraphQL operation results.
Expand All @@ -58,11 +58,17 @@ However, when dealing with HTTP/1 servers from a browser, consider using
the "single connection mode" (`singleConnection = true`) which will
use only one SSE connection.

#### Type parameters

| Name | Type |
| :------ | :------ |
| `SingleConnection` | extends `boolean` = ``false`` |

#### Parameters

| Name | Type |
| :------ | :------ |
| `options` | [`ClientOptions`](interfaces/ClientOptions.md) |
| `options` | [`ClientOptions`](interfaces/ClientOptions.md)<`SingleConnection`\> |

#### Returns

Expand Down
58 changes: 27 additions & 31 deletions docs/interfaces/ClientOptions.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
[graphql-sse](../README.md) / ClientOptions

# Interface: ClientOptions
# Interface: ClientOptions<SingleConnection\>

## Type parameters

| Name | Type |
| :------ | :------ |
| `SingleConnection` | extends `boolean` = ``false`` |

## Table of contents

Expand All @@ -12,14 +18,14 @@
- [headers](ClientOptions.md#headers)
- [lazy](ClientOptions.md#lazy)
- [lazyCloseTimeout](ClientOptions.md#lazyclosetimeout)
- [onNonLazyError](ClientOptions.md#onnonlazyerror)
- [retryAttempts](ClientOptions.md#retryattempts)
- [singleConnection](ClientOptions.md#singleconnection)
- [url](ClientOptions.md#url)

### Methods

- [generateID](ClientOptions.md#generateid)
- [onNonLazyError](ClientOptions.md#onnonlazyerror)
- [retry](ClientOptions.md#retry)

## Properties
Expand Down Expand Up @@ -82,7 +88,7 @@ ___

### lazy

`Optional` **lazy**: `boolean`
`Optional` **lazy**: `SingleConnection` extends ``true`` ? `boolean` : `never`

Controls when should the connection be established while using the
client in "single connection mode" (see `singleConnection ` option).
Expand All @@ -99,7 +105,7 @@ ___

### lazyCloseTimeout

`Optional` **lazyCloseTimeout**: `number`
`Optional` **lazyCloseTimeout**: `SingleConnection` extends ``true`` ? `number` : `never`

How long should the client wait before closing the connection after the last oparation has
completed. You might want to have a calmdown time before actually closing the connection.
Expand All @@ -113,6 +119,22 @@ in "distinct connection mode" (`singleConnection = false`).

___

### onNonLazyError

`Optional` **onNonLazyError**: `SingleConnection` extends ``true`` ? (`error`: `unknown`) => `void` : `never`

Used ONLY when the client is in non-lazy mode (`lazy = false`). When
using this mode, errors might have no sinks to report to; however,
to avoid swallowing errors, `onNonLazyError` will be called when either:
- An unrecoverable error/close event occurs
- Silent retry attempts have been exceeded

After a client has errored out, it will NOT perform any automatic actions.

**`default`** console.error

___

### retryAttempts

`Optional` **retryAttempts**: `number`
Expand All @@ -125,7 +147,7 @@ ___

### singleConnection

`Optional` **singleConnection**: `boolean`
`Optional` **singleConnection**: `SingleConnection`

Reuses a single SSE connection for all GraphQL operations.

Expand Down Expand Up @@ -176,32 +198,6 @@ Reference: https://gist.github.com/jed/982883

___

### onNonLazyError

`Optional` **onNonLazyError**(`error`): `void`

Used ONLY when the client is in non-lazy mode (`lazy = false`). When
using this mode, errors might have no sinks to report to; however,
to avoid swallowing errors, `onNonLazyError` will be called when either:
- An unrecoverable error/close event occurs
- Silent retry attempts have been exceeded

After a client has errored out, it will NOT perform any automatic actions.

**`default`** console.error

#### Parameters

| Name | Type |
| :------ | :------ |
| `error` | `unknown` |

#### Returns

`void`

___

### retry

`Optional` **retry**(`retries`): `Promise`<`void`\>
Expand Down
16 changes: 10 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
export * from './common';

/** @category Client */
export interface ClientOptions {
export interface ClientOptions<SingleConnection extends boolean = false> {
/**
* Reuses a single SSE connection for all GraphQL operations.
*
Expand All @@ -34,7 +34,7 @@ export interface ClientOptions {
*
* @default false
*/
singleConnection?: boolean;
singleConnection?: SingleConnection;
/**
* Controls when should the connection be established while using the
* client in "single connection mode" (see `singleConnection ` option).
Expand All @@ -47,7 +47,7 @@ export interface ClientOptions {
*
* @default true
*/
lazy?: boolean;
lazy?: SingleConnection extends true ? boolean : never;
/**
* How long should the client wait before closing the connection after the last oparation has
* completed. You might want to have a calmdown time before actually closing the connection.
Expand All @@ -59,7 +59,7 @@ export interface ClientOptions {
*
* @default 0
*/
lazyCloseTimeout?: number;
lazyCloseTimeout?: SingleConnection extends true ? number : never;
/**
* Used ONLY when the client is in non-lazy mode (`lazy = false`). When
* using this mode, errors might have no sinks to report to; however,
Expand All @@ -71,7 +71,9 @@ export interface ClientOptions {
*
* @default console.error
*/
onNonLazyError?: (error: unknown) => void;
onNonLazyError?: SingleConnection extends true
? (error: unknown) => void
: never;
/**
* URL of the GraphQL over SSE server to connect.
*
Expand Down Expand Up @@ -186,7 +188,9 @@ export interface Client {
*
* @category Client
*/
export function createClient(options: ClientOptions): Client {
export function createClient<SingleConnection extends boolean = false>(
options: ClientOptions<SingleConnection>,
): Client {
const {
singleConnection = false,
lazy = true,
Expand Down

0 comments on commit be2ae7d

Please sign in to comment.