Skip to content

Commit

Permalink
fix: DTS (issue #250) (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-guggisberg authored Feb 3, 2022
1 parent 95231a6 commit 6ee4081
Showing 1 changed file with 59 additions and 55 deletions.
114 changes: 59 additions & 55 deletions src/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
* governing permissions and limitations under the License.
*/

export declare enum ALPNProtocol {
export enum ALPNProtocol {
ALPN_HTTP2 = 'h2',
ALPN_HTTP2C = 'h2c',
ALPN_HTTP1_1 = 'http/1.1',
ALPN_HTTP1_0 = 'http/1.0',
}

export interface Http1Options {
/**
* Keep sockets around in a pool to be used by other requests in the future.
Expand Down Expand Up @@ -68,7 +69,7 @@ export interface Http1Options {

type HeadersInit = Headers | Object | Iterable<readonly [string, string]> | Iterable<Iterable<string>>;

declare interface RequestInit {
export interface RequestInit {
/**
* A BodyInit object or null to set request's body.
*/
Expand All @@ -83,7 +84,7 @@ declare interface RequestInit {
method?: string;
}

interface ResponseInit {
export interface ResponseInit {
headers?: HeadersInit;
status?: number;
statusText?: string;
Expand All @@ -95,22 +96,22 @@ type BodyInit =
| NodeJS.ReadableStream
| string;

export declare class Headers implements Iterable<[string, string]> {
constructor(init?: HeadersInit);
append(name: string, value: string): void;
delete(name: string): void;
get(name: string): string | null;
has(name: string): boolean;
set(name: string, value: string): void;
entries(): Iterator<[string, string]>;
keys(): Iterator<string>;
values(): Iterator<string>;
[Symbol.iterator](): Iterator<[string, string]>;
}

export declare class Body {
export class Headers implements Iterable<[string, string]> {
constructor(init?: HeadersInit);

append(name: string, value: string): void;
delete(name: string): void;
get(name: string): string | null;
has(name: string): boolean;
set(name: string, value: string): void;

entries(): Iterator<[string, string]>;
keys(): Iterator<string>;
values(): Iterator<string>;
[Symbol.iterator](): Iterator<[string, string]>;
}

export class Body {
constructor(body?: BodyInit);

readonly body: NodeJS.ReadableStream | null;
Expand All @@ -124,14 +125,14 @@ export declare class Body {

type RequestInfo = string | Body;

declare class Request extends Body {
export class Request extends Body {
constructor(input: RequestInfo, init?: RequestInit);
readonly headers: Headers;
readonly method: string;
readonly url: string;
}

export declare class Response extends Body {
export class Response extends Body {
constructor(body?: BodyInit | Object | null, init?: ResponseInit);

readonly url: string;
Expand All @@ -141,19 +142,13 @@ export declare class Response extends Body {
readonly redirected: boolean;
readonly httpVersion: string;
headers: Headers;
}

export type PushPromiseHandler = (
url: string,
headers: NodeJS.Dict<string | string[]>,
reject: () => void
) => void;

export type PushHandler = (
url: string,
headers: NodeJS.Dict<string | string[]>,
response: Response
) => void;
// extensions
/**
* A boolean specifying whether the response was retrieved from the cache.
*/
readonly fromCache?: boolean;
}

export interface Http2Options {
/**
Expand All @@ -166,8 +161,6 @@ export interface Http2Options {
* @default true
*/
enablePush?: boolean;
// pushPromiseHandler?: PushPromiseHandler;
// pushHandler?: PushHandler;
/**
* Max idle time in milliseconds after which a pushed stream will be automatically closed.
* @default 5000
Expand Down Expand Up @@ -218,13 +211,24 @@ export interface ContextOptions {
h2?: Http2Options;
}

type AbortSignal = {
class AbortSignal {

This comment has been minimized.

Copy link
@sjinks

sjinks Feb 3, 2022

Contributor

Unfortunately, this does not work:

node_modules/@adobe/helix-fetch/src/api.d.ts:214:1 - error TS1046: Top-level declarations in .d.ts files must start with either a 'declare' or 'export' modifier.

214 class AbortSignal {
    ~~~~~

This comment has been minimized.

Copy link
@stefan-guggisberg

stefan-guggisberg Feb 4, 2022

Author Contributor

Strange. I tested with a simple client written in TS and I didn't get any error.

readonly aborted: boolean;

addEventListener(type: 'abort', listener: (this: AbortSignal) => void): void;
removeEventListener(type: 'abort', listener: (this: AbortSignal) => void): void;
};

class TimeoutSignal extends AbortSignal {
constructor(timeout: number);

clear(): void;
}

export class AbortController {
readonly signal: AbortSignal;
abort(): void;
};

export interface RequestOptions {
/**
* A string specifying the HTTP request method.
Expand Down Expand Up @@ -270,17 +274,12 @@ export interface RequestOptions {
follow?: number;
}

/**
* @see https://dom.spec.whatwg.org/#interface-abortcontroller
*/
export type AbortController = Window["window"]["AbortController"];

// Errors
export interface FetchBaseError extends Error {
export class FetchBaseError extends Error {
type?: string;
}

interface SystemError {
export type SystemError = {
address?: string;
code: string;
dest?: string;
Expand All @@ -290,13 +289,14 @@ interface SystemError {
path?: string;
port?: number;
syscall: string;
}
};

export interface FetchError extends FetchBaseError{
export class FetchError extends FetchBaseError {
code: number;
erroredSysCall?: SystemError;
}
export interface AbortError extends FetchBaseError{

export class AbortError extends FetchBaseError {
type: 'aborted'
}

Expand All @@ -305,6 +305,11 @@ interface CacheStats {
count: number;
}

export type PushHandler = (
url: string,
response: Response
) => void;

/**
* Fetches a resource from the network. Returns a Promise which resolves once
* the response is available.
Expand All @@ -316,50 +321,49 @@ interface CacheStats {
* @throws {AbortError}
* @throws {TypeError}
*/
export declare function fetch(url: string, options: RequestOptions): Promise<Response>;
export function fetch(url: string|Request, options?: RequestOptions): Promise<Response>;

/**
* Resets the current context, i.e. disconnects all open/pending sessions, clears caches etc..
*/
export declare function reset(): Promise<[void, void]>;
export function reset(): Promise<[void, void]>;

/**
* Register a callback which gets called once a server Push has been received.
*
* @param {PushHandler} fn callback function invoked with the url and the pushed Response
*/
export declare function onPush(fn: PushHandler): void;
export function onPush(fn: PushHandler): void;

/**
* Deregister a callback previously registered with {#onPush}.
*
* @param {PushHandler} fn callback function registered with {#onPush}
*/
export declare function offPush(fn: PushHandler): void;
export function offPush(fn: PushHandler): void;

/**
* Create a URL with query parameters
*
* @param {string} url request url
* @param {object} [qs={}] request query parameters
*/
export declare function createUrl(url: string, qs?: Record<string, unknown>): string;
export function createUrl(url: string, qs?: Record<string, unknown>): string;

/**
* Creates a timeout signal which allows to specify
* a timeout for a `fetch` operation via the `signal` option.
*
* @param {number} ms timeout in milliseconds
*/
export declare function timeoutSignal(ms: number): AbortSignal;
export function timeoutSignal(ms: number): TimeoutSignal;

/**
* Clear the cache entirely, throwing away all values.
*/
export declare function clearCache(): void;
export function clearCache(): void;

/**
* Cache stats for diagnostic purposes
*/
export declare function cacheStats(): CacheStats;

export function cacheStats(): CacheStats;

0 comments on commit 6ee4081

Please sign in to comment.