Skip to content

Commit

Permalink
fix(client): fix TS errors that appear when users Go to Source in VSC…
Browse files Browse the repository at this point in the history
…ode (#142)
  • Loading branch information
stainless-bot authored and rattrayalex committed Sep 12, 2023
1 parent 49b4bf7 commit 45d970b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ We are keen for your feedback; please open an [issue](https://www.github.com/ant

## Requirements

TypeScript >= 4.5 is supported.

The following runtimes are supported:

- Node.js 16 LTS or later ([non-EOL](https://endoflife.date/nodejs)) versions.
Expand Down
10 changes: 5 additions & 5 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ node scripts/fix-index-exports.cjs
# index.d.mts the default import will work (even though both files have
# the same export default statement)
cp dist/index.d.ts dist/index.d.mts

SED=(sed -i)
if [[ "$OSTYPE" == "darwin"* ]]; then SED=(sed -i ''); fi
cp tsconfig.dist-src.json dist/src/tsconfig.json

# strip out lib="dom" and types="node" references; these are needed at build time,
# but would pollute the user's TS environment
REFERENCE_SUBS='s/^ *\/\/\/ *<reference *lib="dom".*//g;s/^ *\/\/\/ *<reference *types="node".*//g'
find dist -type f -exec "${SED[@]}" "${REFERENCE_SUBS}" {} +
find dist -type f -exec node scripts/remove-triple-slash-references.js {} +
# strip out `unknown extends RequestInit ? never :` from dist/src/_shims;
# these cause problems when viewing the .ts source files in go to definition
find dist/src/_shims -type f -exec node scripts/replace-shim-guards.js {} +

npm exec prettier -- --loglevel=warn --write .

Expand Down
11 changes: 11 additions & 0 deletions scripts/remove-triple-slash-references.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// strip out lib="dom" and types="node" references; these are needed at build time,
// but would pollute the user's TS environment
const fs = require('fs');
for (const file of process.argv.slice(2)) {
const before = fs.readFileSync(file, 'utf8');
const after = before.replace(/^ *\/\/\/ *<reference +(lib="dom"|types="node").*?\n/gm, '');
if (after !== before) {
fs.writeFileSync(file, after, 'utf8');
console.error('wrote', file);
}
}
14 changes: 14 additions & 0 deletions scripts/replace-shim-guards.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// strip out `unknown extends RequestInit ? never :` from dist/src/_shims;
// these cause problems when viewing the .ts source files in go to definition
const fs = require('fs');
for (const file of process.argv.slice(2)) {
const before = fs.readFileSync(file, 'utf8');
const after = before.replace(
new RegExp('unknown extends (typeof )?\\S+ \\? \\S+ :\\s*'.replace(/\s+/, '\\s+'), 'gm'),
'',
);
if (after !== before) {
fs.writeFileSync(file, after, 'utf8');
console.error('wrote', file);
}
}
4 changes: 4 additions & 0 deletions src/_shims/fetch-deno.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const _fetch = fetch;
type _fetch = typeof fetch;
const _Request = Request;
type _Request = Request;
type _RequestInfo = RequestInfo;
type _RequestInit = RequestInit;
const _Response = Response;
type _Response = Response;
type _ResponseInit = ResponseInit;
type _BodyInit = BodyInit;
const _Headers = Headers;
type _Headers = Headers;
type _HeadersInit = HeadersInit;

export const isPolyfilled = false;
Expand Down
3 changes: 3 additions & 0 deletions src/_shims/form-data-deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ type _BlobPropertyBag = BlobPropertyBag;
type _FilePropertyBag = FilePropertyBag;

const _FormData = FormData;
type _FormData = FormData;
const _File = File;
type _File = File;
const _Blob = Blob;
type _Blob = Blob;

export const isPolyfilled = false;

Expand Down
7 changes: 4 additions & 3 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async function defaultParseResponse<T>(props: APIResponseProps): Promise<T> {
// TODO handle blob, arraybuffer, other content types, etc.
const text = await response.text();
debug('response', response.status, response.url, response.headers, text);
return text as T;
return text as any as T;
}

/**
Expand Down Expand Up @@ -313,7 +313,8 @@ export abstract class APIClient {
protected parseHeaders(headers: HeadersInit | null | undefined): Record<string, string> {
return (
!headers ? {}
: Symbol.iterator in headers ? Object.fromEntries(Array.from(headers).map((header) => [...header]))
: Symbol.iterator in headers ?
Object.fromEntries(Array.from(headers as Iterable<string[]>).map((header) => [...header]))
: { ...headers }
);
}
Expand Down Expand Up @@ -397,7 +398,7 @@ export abstract class APIClient {
return new PagePromise<PageClass, Item>(this, request, Page);
}

buildURL<Req>(path: string, query: Req | undefined): string {
buildURL<Req extends Record<string, unknown>>(path: string, query: Req | null | undefined): string {
const url =
isAbsoluteURL(path) ?
new URL(path)
Expand Down
11 changes: 11 additions & 0 deletions tsconfig.dist-src.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
// this config is included in the published src directory to prevent TS errors
// from appearing when users go to source, and VSCode opens the source .ts file
// via declaration maps
"include": ["index.ts"],
"compilerOptions": {
"target": "es2015",
"lib": ["DOM"],
"moduleResolution": "node"
}
}

0 comments on commit 45d970b

Please sign in to comment.