Skip to content

Commit cab6ad2

Browse files
chore(internal): fix format script
1 parent ab904f6 commit cab6ad2

11 files changed

Lines changed: 206 additions & 90 deletions

File tree

.devcontainer/devcontainer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
"postCreateCommand": "yarn install",
1010
"customizations": {
1111
"vscode": {
12-
"extensions": [
13-
"esbenp.prettier-vscode"
14-
]
12+
"extensions": ["esbenp.prettier-vscode"]
1513
}
1614
}
1715
}

.github/workflows/release-doctor.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,3 @@ jobs:
1919
bash ./bin/check-release-environment
2020
env:
2121
NPM_TOKEN: ${{ secrets.WRITER_NPM_TOKEN || secrets.NPM_TOKEN }}
22-

MIGRATION.md

Lines changed: 97 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,95 +4,40 @@ This guide outlines the changes and steps needed to migrate your codebase to the
44

55
The main changes are that the SDK now relies on the [builtin Web fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) instead of `node-fetch` and has zero dependencies.
66

7+
## Migration CLI
8+
9+
Most programs will only need minimal changes, but to assist there is a migration tool that will automatically update your code for the new version.
10+
To use it, upgrade the `writer-sdk` package, then run `./node_modules/.bin/writer-sdk migrate ./your/src/folders` to update your code.
11+
To preview the changes without writing them to disk, run the tool with `--dry`.
12+
713
## Environment requirements
814

915
The minimum supported runtime and tooling versions are now:
1016

11-
- Node.js 18.x last LTS (Required for built-in fetch support)
17+
- Node.js 18.x last LTS (Required for builtin fetch support)
1218
- This was previously documented as the minimum supported Node.js version but Node.js 16.x mostly worked at runtime; now it will not.
1319
- TypeScript 4.9
1420
- Jest 28
1521

16-
## Minimum types requirements
17-
18-
### DOM
19-
20-
`tsconfig.json`
21-
22-
```jsonc
23-
{
24-
"target": "ES2015", // note: we recommend ES2020 or higher
25-
"lib": ["DOM", "DOM.Iterable", "ES2018"]
26-
}
27-
```
28-
29-
### Node.js
30-
31-
`tsconfig.json`
32-
33-
```jsonc
34-
{
35-
"target": "ES2015" // note: we recommend ES2020 or higher
36-
}
37-
```
38-
39-
`package.json`
40-
41-
```json
42-
{
43-
"devDependencies": {
44-
"@types/node": ">= 18.18.7"
45-
}
46-
}
47-
```
48-
49-
### Cloudflare Workers
50-
51-
`tsconfig.json`
52-
53-
```jsonc
54-
{
55-
"target": "ES2015", // note: we recommend ES2020 or higher
56-
"lib": ["ES2020"], // <- needed by @cloudflare/workers-types
57-
"types": ["@cloudflare/workers-types"]
58-
}
59-
```
60-
61-
`package.json`
62-
63-
```json
64-
{
65-
"devDependencies": {
66-
"@cloudflare/workers-types": ">= 0.20221111.0"
67-
}
68-
}
69-
```
70-
71-
### Bun
22+
## Breaking changes
7223

73-
`tsconfig.json`
24+
### Web types for `withResponse`, `asResponse`, and `APIError.headers`
7425

75-
```jsonc
76-
{
77-
"target": "ES2015" // note: we recommend ES2020 or higher
78-
}
79-
```
80-
81-
`package.json`
26+
Because we now use the builtin Web fetch API on all platforms, if you wrote code that used `withResponse` or `asResponse` and then accessed `node-fetch`-specific properties on the result, you will need to switch to standardized alternatives.
27+
For example, `body` is now a [Web `ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) rather than a [node `Readable`](https://nodejs.org/api/stream.html#readable-streams).
8228

83-
```json
84-
{
85-
"devDependencies": {
86-
"@types/bun": ">= 1.2.0"
87-
}
88-
}
29+
```ts
30+
// Before:
31+
const res = await client.example.retrieve('string/with/slash').asResponse();
32+
res.body.pipe(process.stdout);
33+
34+
// After:
35+
import { Readable } from 'node:stream';
36+
const res = await client.example.retrieve('string/with/slash').asResponse();
37+
Readable.fromWeb(res.body).pipe(process.stdout);
8938
```
9039

91-
### Deno
92-
93-
No config needed!
94-
95-
## Breaking changes
40+
Additionally, the `headers` property on `APIError` objects is now an instance of the Web [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) class. It was previously defined as `Record<string, string | null | undefined>`.
9641

9742
### Named path parameters
9843

@@ -355,6 +300,80 @@ import Writer from 'writer-sdk/src';
355300
import Writer from 'writer-sdk';
356301
```
357302

358-
### Headers
303+
## TypeScript troubleshooting
304+
305+
When referencing the library after updating, you may encounter new type errors related to JS features like private properties or fetch classes like Request, Response, and Headers.
306+
To resolve these issues, configure your tsconfig.json and install the appropriate `@types` packages for your runtime environment using the guidelines below:
307+
308+
### Browsers
309+
310+
`tsconfig.json`
311+
312+
```jsonc
313+
{
314+
"target": "ES2018", // note: we recommend ES2020 or higher
315+
"lib": ["DOM", "DOM.Iterable", "ES2018"]
316+
}
317+
```
318+
319+
### Node.js
359320

360-
The `headers` property on `APIError` objects is now an instance of the Web [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) class. It was previously just `Record<string, string | null | undefined>`.
321+
`tsconfig.json`
322+
323+
```jsonc
324+
{
325+
"target": "ES2018" // note: we recommend ES2020 or higher
326+
}
327+
```
328+
329+
`package.json`
330+
331+
```json
332+
{
333+
"devDependencies": {
334+
"@types/node": ">= 18.18.7"
335+
}
336+
}
337+
```
338+
339+
### Cloudflare Workers
340+
341+
`tsconfig.json`
342+
343+
```jsonc
344+
{
345+
"target": "ES2018", // note: we recommend ES2020 or higher
346+
"lib": ["ES2020"], // <- needed by @cloudflare/workers-types
347+
"types": ["@cloudflare/workers-types"]
348+
}
349+
```
350+
351+
`package.json`
352+
353+
```json
354+
{
355+
"devDependencies": {
356+
"@cloudflare/workers-types": ">= 0.20221111.0"
357+
}
358+
}
359+
```
360+
361+
### Bun
362+
363+
`tsconfig.json`
364+
365+
```jsonc
366+
{
367+
"target": "ES2018" // note: we recommend ES2020 or higher
368+
}
369+
```
370+
371+
`package.json`
372+
373+
```json
374+
{
375+
"devDependencies": {
376+
"@types/bun": ">= 1.2.0"
377+
}
378+
}
379+
```

bin/cli

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env node
2+
3+
const { spawnSync } = require('child_process');
4+
5+
const commands = {
6+
migrate: {
7+
description: 'Run migrations to update to the latest major SDK version',
8+
fn: () => {
9+
const result = spawnSync(
10+
'npx',
11+
[
12+
'-y',
13+
'https://github.com/stainless-api/migrate-ts/releases/download/0.0.2/stainless-api-migrate-0.0.2-6.tgz',
14+
'--migrationConfig',
15+
require.resolve('./migration-config.json'),
16+
...process.argv.slice(3),
17+
],
18+
{ stdio: 'inherit' },
19+
);
20+
if (result.status !== 0) {
21+
process.exit(result.status);
22+
}
23+
},
24+
},
25+
};
26+
27+
function exitWithHelp() {
28+
console.log(`Usage: writer-sdk <subcommand>`);
29+
console.log();
30+
console.log('Subcommands:');
31+
32+
for (const [name, info] of Object.entries(commands)) {
33+
console.log(` ${name} ${info.description}`);
34+
}
35+
36+
console.log();
37+
process.exit(1);
38+
}
39+
40+
if (process.argv.length < 3) {
41+
exitWithHelp();
42+
}
43+
44+
const commandName = process.argv[2];
45+
46+
const command = commands[commandName];
47+
if (!command) {
48+
console.log(`Unknown subcommand ${commandName}.`);
49+
exitWithHelp();
50+
}
51+
52+
command.fn();

bin/migration-config.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"pkg": "writer-sdk",
3+
"githubRepo": "https://github.com/writer/writer-node",
4+
"clientClass": "Writer",
5+
"methods": [
6+
{
7+
"base": "graphs",
8+
"name": "removeFileFromGraph",
9+
"params": [
10+
{
11+
"type": "param",
12+
"key": "file_id",
13+
"location": "path"
14+
},
15+
{
16+
"type": "params",
17+
"maybeOverload": false
18+
},
19+
{
20+
"type": "options"
21+
}
22+
],
23+
"oldParams": [
24+
{
25+
"type": "param",
26+
"key": "graph_id",
27+
"location": "path"
28+
},
29+
{
30+
"type": "param",
31+
"key": "file_id",
32+
"location": "path"
33+
},
34+
{
35+
"type": "options"
36+
}
37+
]
38+
}
39+
]
40+
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"test": "./scripts/test",
1818
"build": "./scripts/build",
1919
"prepublishOnly": "echo 'to publish, run yarn build && (cd dist; yarn publish)' && exit 1",
20-
"format": "prettier --write --cache --cache-strategy metadata . !dist",
20+
"format": "./scripts/format",
2121
"prepare": "if ./scripts/utils/check-is-in-git-install.sh; then ./scripts/build && ./scripts/utils/git-swap.sh; fi",
2222
"tsn": "ts-node -r tsconfig-paths/register",
2323
"lint": "./scripts/lint",
@@ -59,6 +59,9 @@
5959
"writer-sdk": ".",
6060
"writer-sdk/*": "./src/*"
6161
},
62+
"bin": {
63+
"writer-sdk": "bin/cli"
64+
},
6265
"exports": {
6366
".": {
6467
"import": "./dist/index.mjs",

release-please-config.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,5 @@
6060
}
6161
],
6262
"release-type": "node",
63-
"extra-files": [
64-
"src/version.ts",
65-
"README.md"
66-
]
63+
"extra-files": ["src/version.ts", "README.md"]
6764
}

scripts/build

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ for file in LICENSE CHANGELOG.md; do
1919
if [ -e "${file}" ]; then cp "${file}" dist; fi
2020
done
2121
if [ -e "bin/cli" ]; then
22-
mkdir dist/bin
22+
mkdir -p dist/bin
2323
cp -p "bin/cli" dist/bin/;
2424
fi
25+
if [ -e "bin/migration-config.json" ]; then
26+
mkdir -p dist/bin
27+
cp -p "bin/migration-config.json" dist/bin/;
28+
fi
2529
# this converts the export map paths for the dist directory
2630
# and does a few other minor things
2731
node scripts/utils/make-dist-package-json.cjs > dist/package.json

scripts/format

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ cd "$(dirname "$0")/.."
66

77
echo "==> Running eslint --fix"
88
./node_modules/.bin/eslint --fix .
9+
10+
echo "==> Running prettier --write"
11+
# format things eslint didn't
12+
./node_modules/.bin/prettier --write --cache --cache-strategy metadata . '!**/dist' '!**/*.ts' '!**/*.mts' '!**/*.cts' '!**/*.js' '!**/*.mjs' '!**/*.cjs'

tsconfig.build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"rootDir": "./dist/src",
77
"paths": {
88
"writer-sdk/*": ["dist/src/*"],
9-
"writer-sdk": ["dist/src/index.ts"],
9+
"writer-sdk": ["dist/src/index.ts"]
1010
},
1111
"noEmit": false,
1212
"declaration": true,

0 commit comments

Comments
 (0)