Skip to content

Commit

Permalink
feat(client): Accept referrer and referrerPolicy fetch options (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
santino committed Jul 20, 2022
1 parent 856920a commit dbaa90a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
36 changes: 36 additions & 0 deletions docs/interfaces/ClientOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
- [lazyCloseTimeout](ClientOptions.md#lazyclosetimeout)
- [onMessage](ClientOptions.md#onmessage)
- [onNonLazyError](ClientOptions.md#onnonlazyerror)
- [referrer](ClientOptions.md#referrer)
- [referrerPolicy](ClientOptions.md#referrerpolicy)
- [retry](ClientOptions.md#retry)
- [retryAttempts](ClientOptions.md#retryattempts)
- [singleConnection](ClientOptions.md#singleconnection)
Expand Down Expand Up @@ -192,6 +194,40 @@ console.error

___

### referrer

`Optional` **referrer**: `string`

A string specifying the referrer of the request. This can be a same-origin URL, about:client, or an empty string.

**`Default`**

undefined

___

### referrerPolicy

`Optional` **referrerPolicy**: ``"same-origin"`` \| ``"no-referrer"`` \| ``"no-referrer-when-downgrade"`` \| ``"origin"`` \| ``"strict-origin"`` \| ``"origin-when-cross-origin"`` \| ``"strict-origin-when-cross-origin"`` \| ``"unsafe-url"``

Specifies the referrer policy to use for the request.

Possible options are:
- `no-referrer`: Does not send referrer information along with requests to any origin.
- `no-referrer-when-downgrade`: Sends full referrerURL for requests: whose referrerURL and current URL are both potentially trustworthy URLs, or whose referrerURL is a non-potentially trustworthy URL.
- `same-origin`: Sends full referrerURL as referrer information when making same-origin-referrer requests.
- `origin`: Sends only the ASCII serialization of the request’s referrerURL when making both same-origin-referrer requests and cross-origin-referrer requests.
- `strict-origin`: Sends the ASCII serialization of the origin of the referrerURL for requests: whose referrerURL and current URL are both potentially trustworthy URLs, or whose referrerURL is a non-potentially trustworthy URL
- `origin-when-cross-origin`: Sends full referrerURL when making same-origin-referrer requests, and only the ASCII serialization of the origin of the request’s referrerURL is sent when making cross-origin-referrer requests
- `strict-origin-when-cross-origin`: Sends full referrerURL when making same-origin-referrer requests, and only the ASCII serialization of the origin of the request’s referrerURL when making cross-origin-referrer requests: whose referrerURL and current URL are both potentially trustworthy URLs, or whose referrerURL is a non-potentially trustworthy URL.
- `unsafe-url`: Sends full referrerURL along for both same-origin-referrer requests and cross-origin-referrer requests.

**`Default`**

undefined

___

### retry

`Optional` **retry**: (`retries`: `number`) => `Promise`<`void`\>
Expand Down
40 changes: 39 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,28 @@ export interface ClientOptions<SingleConnection extends boolean = false> {
* @default same-origin
*/
credentials?: 'omit' | 'same-origin' | 'include';
/**
* A string specifying the referrer of the request. This can be a same-origin URL, about:client, or an empty string.
*
* @default undefined
*/
referrer?: string;
/**
* Specifies the referrer policy to use for the request.
*
* Possible options are:
* - `no-referrer`: Does not send referrer information along with requests to any origin.
* - `no-referrer-when-downgrade`: Sends full referrerURL for requests: whose referrerURL and current URL are both potentially trustworthy URLs, or whose referrerURL is a non-potentially trustworthy URL.
* - `same-origin`: Sends full referrerURL as referrer information when making same-origin-referrer requests.
* - `origin`: Sends only the ASCII serialization of the request’s referrerURL when making both same-origin-referrer requests and cross-origin-referrer requests.
* - `strict-origin`: Sends the ASCII serialization of the origin of the referrerURL for requests: whose referrerURL and current URL are both potentially trustworthy URLs, or whose referrerURL is a non-potentially trustworthy URL
* - `origin-when-cross-origin`: Sends full referrerURL when making same-origin-referrer requests, and only the ASCII serialization of the origin of the request’s referrerURL is sent when making cross-origin-referrer requests
* - `strict-origin-when-cross-origin`: Sends full referrerURL when making same-origin-referrer requests, and only the ASCII serialization of the origin of the request’s referrerURL when making cross-origin-referrer requests: whose referrerURL and current URL are both potentially trustworthy URLs, or whose referrerURL is a non-potentially trustworthy URL.
* - `unsafe-url`: Sends full referrerURL along for both same-origin-referrer requests and cross-origin-referrer requests.
*
* @default undefined
*/
referrerPolicy?: 'no-referrer' | 'no-referrer-when-downgrade' | 'same-origin' | 'origin' | 'strict-origin' | 'origin-when-cross-origin' | 'strict-origin-when-cross-origin' | 'unsafe-url';
/**
* HTTP headers to pass along the request.
*
Expand Down Expand Up @@ -234,6 +256,8 @@ export function createClient<SingleConnection extends boolean = false>(
);
},
credentials = 'same-origin',
referrer,
referrerPolicy,
onMessage,
} = options;
const fetchFn = (options.fetchFn || fetch) as typeof fetch;
Expand Down Expand Up @@ -322,6 +346,8 @@ export function createClient<SingleConnection extends boolean = false>(
signal: connCtrl.signal,
method: 'PUT',
credentials,
referrer,
referrerPolicy,
headers,
});
} catch (err) {
Expand All @@ -336,6 +362,8 @@ export function createClient<SingleConnection extends boolean = false>(
signal: connCtrl.signal,
headers,
credentials,
referrer,
referrerPolicy,
url,
fetchFn,
onMessage,
Expand Down Expand Up @@ -427,6 +455,8 @@ export function createClient<SingleConnection extends boolean = false>(
signal: control.signal,
headers,
credentials,
referrer,
referrerPolicy,
url,
body: JSON.stringify(request),
fetchFn,
Expand Down Expand Up @@ -491,6 +521,8 @@ export function createClient<SingleConnection extends boolean = false>(
signal: control.signal,
method: 'POST',
credentials,
referrer,
referrerPolicy,
headers,
body: JSON.stringify(request),
});
Expand All @@ -511,6 +543,8 @@ export function createClient<SingleConnection extends boolean = false>(
signal: control.signal,
method: 'DELETE',
credentials,
referrer,
referrerPolicy,
headers,
});
} catch (err) {
Expand Down Expand Up @@ -647,6 +681,8 @@ interface ConnectOptions<SingleConnection extends boolean> {
signal: AbortSignal;
url: string;
credentials: 'omit' | 'same-origin' | 'include';
referrer?: string;
referrerPolicy?: 'no-referrer' | 'no-referrer-when-downgrade' | 'same-origin' | 'origin' | 'strict-origin' | 'origin-when-cross-origin' | 'strict-origin-when-cross-origin' | 'unsafe-url';
headers?: Record<string, string> | undefined;
body?: string;
fetchFn: typeof fetch;
Expand All @@ -658,7 +694,7 @@ interface ConnectOptions<SingleConnection extends boolean> {
async function connect<SingleConnection extends boolean>(
options: ConnectOptions<SingleConnection>,
): Promise<Connection> {
const { signal, url, credentials, headers, body, fetchFn, onMessage } =
const { signal, url, credentials, headers, body, referrer, referrerPolicy, fetchFn, onMessage } =
options;

const waiting: {
Expand All @@ -674,6 +710,8 @@ async function connect<SingleConnection extends boolean>(
signal,
method: body ? 'POST' : 'GET',
credentials,
referrer,
referrerPolicy,
headers: {
...headers,
accept: 'text/event-stream',
Expand Down

0 comments on commit dbaa90a

Please sign in to comment.