Skip to content

Commit

Permalink
fix: lib.fetch types using experimental ts-graft discover mode
Browse files Browse the repository at this point in the history
lib.fetch.d.ts includes all type definitions from the dom and
dom.iterable libs which were reachable from the original cross-fetch
index.d.ts file:

```
git checkout 7b19cda -- index.d.ts
```

lib.fetch.d.ts was generated with the following command:

```
cat <<EOF > .ts-graftrc.yaml \
&& npx ts-graft@2.0.0-1 \
&& rm .ts-graftrc.yaml \
grafts:
  - source: index.d.ts
    output: lib.fetch.d.ts
    include:
      - dom
      - dom.iterable
EOF
```

index.d.ts was then updated to its new state and verified with the
following command:

```
npx tsc --lib ES6 index.d.ts
```

Issue lquixada#95 was validated as follows:

```
cat <<EOF > test.ts \
&& npx tsc --target ES6 --moduleResolution node --noEmit  test.ts \
&& rm test.ts
import fetch from "./";
export const customFetch = (
  input: RequestInfo,
  init: RequestInit,
): Promise<Response> => {
  let url = "";
  url += input;
  return fetch(url, init);
};
EOF
```

n.b. consumers must include the dom.iterable lib, which is implied by
the ES6 target, lest their globals lack the required members defined
by dom.iterable.
  • Loading branch information
jstewmon committed Mar 22, 2021
1 parent d46e33f commit c08d9c7
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 257 deletions.
47 changes: 14 additions & 33 deletions index.d.ts
@@ -1,33 +1,14 @@
import type {
BodyInit,
Headers as BaseHeaders,
HeadersInit,
Request,
RequestInfo,
RequestInit,
Response,
ResponseInit,
} from "./lib.fetch"
import type { Headers as IterHeaders } from "./lib.fetch.iterable";
type Headers = BaseHeaders & IterHeaders;

export const fetch: (input: RequestInfo, init?: RequestInit) => Promise<Response>;

export const Request: {
prototype: Request;
new(input: RequestInfo, init?: RequestInit): Request;
};

export const Response: {
prototype: Response;
new(body?: BodyInit | null, init?: ResponseInit): Response;
error(): Response;
redirect(url: string, status?: number): Response;
};

export const Headers: {
prototype: Headers;
new(init?: HeadersInit): Headers;
};

export default fetch;
/// <reference lib="dom" />

declare const _fetch: typeof fetch;
declare const _Request: typeof Request;
declare const _Response: typeof Response;
declare const _Headers: typeof Headers;

declare module "cross-fetch" {
export const fetch: typeof _fetch;
export const Request: typeof _Request;
export const Response: typeof _Response;
export const Headers: typeof _Headers;
export default fetch;
}

0 comments on commit c08d9c7

Please sign in to comment.