Skip to content

Commit

Permalink
release: 0.25.0 (#467)
Browse files Browse the repository at this point in the history
Co-authored-by: stainless-bot <dev@stainlessapi.com>
Co-authored-by: stainless-app[bot] <142633134+stainless-app[bot]@users.noreply.github.com>
  • Loading branch information
stainless-app[bot] and stainless-bot authored Jan 7, 2025
1 parent 849402e commit 3ea414f
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.24.0"
".": "0.25.0"
}
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
configured_endpoints: 26
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/val-town%2Fval-town-a2ff8d1d2f7e6375a8d422e67adf9ff5958e010c3ad152d837cf19f79d9a533e.yml
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/val-town%2Fval-town-f02ed5fb5a61e227c08148c44c8c65de2890bab8732d0ab2888714c8f0513e1a.yml
30 changes: 30 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
# Changelog

## 0.25.0 (2025-01-07)

Full Changelog: [v0.24.0...v0.25.0](https://github.com/val-town/sdk/compare/v0.24.0...v0.25.0)

### Features

* **api:** api update ([#477](https://github.com/val-town/sdk/issues/477)) ([7126ec7](https://github.com/val-town/sdk/commit/7126ec7aef2b7f5b6a9c99a6dfe44b18a3f1b92e))
* **internal:** make git install file structure match npm ([#466](https://github.com/val-town/sdk/issues/466)) ([2921499](https://github.com/val-town/sdk/commit/2921499332800264d819b16704980802a746d27a))


### Bug Fixes

* **client:** normalize method ([#475](https://github.com/val-town/sdk/issues/475)) ([00cd997](https://github.com/val-town/sdk/commit/00cd997fc0763ba2fe2e8c83ae9ce28c2ce7ec4b))


### Chores

* **internal:** codegen related update ([#468](https://github.com/val-town/sdk/issues/468)) ([89670f1](https://github.com/val-town/sdk/commit/89670f1110f52d75764d5bd0ef6f93904dcd0b7d))
* **internal:** codegen related update ([#469](https://github.com/val-town/sdk/issues/469)) ([87f8db8](https://github.com/val-town/sdk/commit/87f8db8117438438374f760f0cf95ab9cbbdfdd0))
* **internal:** codegen related update ([#470](https://github.com/val-town/sdk/issues/470)) ([032bec5](https://github.com/val-town/sdk/commit/032bec5c8a9c118c68ee432e6a91d4fa5107e3a1))
* **internal:** codegen related update ([#473](https://github.com/val-town/sdk/issues/473)) ([84a52e1](https://github.com/val-town/sdk/commit/84a52e177691cc284856541d940b7bb2d1c840e6))
* **internal:** codegen related update ([#474](https://github.com/val-town/sdk/issues/474)) ([1db3338](https://github.com/val-town/sdk/commit/1db333806833f23172d2e1045eab28493e07152f))
* **internal:** fix some typos ([#472](https://github.com/val-town/sdk/issues/472)) ([8214c09](https://github.com/val-town/sdk/commit/8214c096e44e4c5295fb2008047d16f3cc80ccf5))
* **internal:** update isAbsoluteURL ([#471](https://github.com/val-town/sdk/issues/471)) ([0cb8e25](https://github.com/val-town/sdk/commit/0cb8e252c0e874660526d28caabf12c5fb648d70))


### Documentation

* minor formatting changes ([#476](https://github.com/val-town/sdk/issues/476)) ([43ce72f](https://github.com/val-town/sdk/commit/43ce72f81519b4d51e230b200e0e78929ad760f3))

## 0.24.0 (2024-11-21)

Full Changelog: [v0.23.0...v0.24.0](https://github.com/val-town/sdk/compare/v0.23.0...v0.24.0)
Expand Down
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Setting up the environment

This repository uses [`yarn@v1`](https://classic.yarnpkg.com/lang/en/docs/install/#mac-stable).
This repository uses [`yarn@v1`](https://classic.yarnpkg.com/lang/en/docs/install).
Other package managers may work but are not officially supported for development.

To set up the repository, run:
Expand Down Expand Up @@ -29,10 +29,10 @@ All files in the `examples/` directory are not modified by the generator and can
```

```
chmod +x examples/<your-example>.ts
```sh
$ chmod +x examples/<your-example>.ts
# run the example against your api
yarn tsn -T examples/<your-example>.ts
$ yarn tsn -T examples/<your-example>.ts
```

## Using the repository from source
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2024 val-town
Copyright 2025 val-town

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@valtown/sdk",
"version": "0.24.0",
"version": "0.25.0",
"exports": "./index.ts",
"publish": {
"exclude": [
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@valtown/sdk",
"version": "0.24.0",
"version": "0.25.0",
"description": "The official TypeScript library for the Val Town API",
"author": "Val Town <support@val.town>",
"types": "dist/index.d.ts",
Expand All @@ -18,7 +18,7 @@
"build": "./scripts/build",
"prepublishOnly": "echo 'to publish, run yarn build && (cd dist; yarn publish)' && exit 1",
"format": "prettier --write --cache --cache-strategy metadata . !dist",
"prepare": "if ./scripts/utils/check-is-in-git-install.sh; then ./scripts/build; fi",
"prepare": "if ./scripts/utils/check-is-in-git-install.sh; then ./scripts/build && ./scripts/utils/git-swap.sh; fi",
"tsn": "ts-node -r tsconfig-paths/register",
"lint": "./scripts/lint",
"fix": "./scripts/format"
Expand Down
2 changes: 2 additions & 0 deletions scripts/build-deno
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ done
for file in README.md LICENSE CHANGELOG.md; do
if [ -e "${file}" ]; then cp "${file}" dist-deno; fi
done

node scripts/utils/convert-jsr-readme.cjs ./dist-deno/README.md
2 changes: 1 addition & 1 deletion scripts/utils/check-is-in-git-install.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
# Check if you happen to call prepare for a repository that's already in node_modules.
[ "$(basename "$(dirname "$PWD")")" = 'node_modules' ] ||
# The name of the containing directory that 'npm` uses, which looks like
Expand Down
140 changes: 140 additions & 0 deletions scripts/utils/convert-jsr-readme.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
const fs = require('fs');
const { parse } = require('@typescript-eslint/parser');
const { TSError } = require('@typescript-eslint/typescript-estree');

/**
* Quick and dirty AST traversal
*/
function traverse(node, visitor) {
if (!node || typeof node.type !== 'string') return;
visitor.node?.(node);
visitor[node.type]?.(node);
for (const key in node) {
const value = node[key];
if (Array.isArray(value)) {
for (const elem of value) traverse(elem, visitor);
} else if (value instanceof Object) {
traverse(value, visitor);
}
}
}

/**
* Helper method for replacing arbitrary ranges of text in input code.
*/
function replaceRanges(code, replacer) {
const replacements = [];
replacer({ replace: (range, replacement) => replacements.push({ range, replacement }) });

if (!replacements.length) return code;
replacements.sort((a, b) => a.range[0] - b.range[0]);
const overlapIndex = replacements.findIndex(
(r, index) => index > 0 && replacements[index - 1].range[1] > r.range[0],
);
if (overlapIndex >= 0) {
throw new Error(
`replacements overlap: ${JSON.stringify(replacements[overlapIndex - 1])} and ${JSON.stringify(
replacements[overlapIndex],
)}`,
);
}

const parts = [];
let end = 0;
for (const {
range: [from, to],
replacement,
} of replacements) {
if (from > end) parts.push(code.substring(end, from));
parts.push(replacement);
end = to;
}
if (end < code.length) parts.push(code.substring(end));
return parts.join('');
}

function replaceProcessEnv(content) {
// Replace process.env['KEY'] and process.env.KEY with Deno.env.get('KEY')
return content.replace(/process\.env(?:\.|\[['"])(.+?)(?:['"]\])/g, "Deno.env.get('$1')");
}

function replaceProcessStdout(content) {
return content.replace(/process\.stdout.write\(([^)]+)\)/g, 'Deno.stdout.writeSync($1)');
}

function replaceInstallationDirections(content) {
// Remove npm installation section
return content.replace(/```sh\nnpm install.*?\n```.*### Installation from JSR\n\n/s, '');
}

/**
* Maps over module paths in imports and exports
*/
function replaceImports(code, config) {
try {
const ast = parse(code, { sourceType: 'module', range: true });
return replaceRanges(code, ({ replace }) =>
traverse(ast, {
node(node) {
switch (node.type) {
case 'ImportDeclaration':
case 'ExportNamedDeclaration':
case 'ExportAllDeclaration':
case 'ImportExpression':
if (node.source) {
const { range, value } = node.source;
if (value.startsWith(config.npm)) {
replace(range, JSON.stringify(value.replace(config.npm, config.jsr)));
}
}
}
},
}),
);
} catch (e) {
if (e instanceof TSError) {
// This can error if the code block is not valid TS, in this case give up trying to transform the imports.
console.warn(`Original codeblock could not be parsed, replace import skipped: ${e}\n\n${code}`);
return code;
}
throw e;
}
}

function processReadme(config, file) {
try {
let readmeContent = fs.readFileSync(file, 'utf8');

// First replace installation directions
readmeContent = replaceInstallationDirections(readmeContent);

// Replace content in all code blocks with a single regex
readmeContent = readmeContent.replaceAll(
/```(?:typescript|ts|javascript|js)\n([\s\S]*?)```/g,
(match, codeBlock) => {
try {
let transformedCode = codeBlock.trim();
transformedCode = replaceImports(transformedCode, config);
transformedCode = replaceProcessEnv(transformedCode);
transformedCode = replaceProcessStdout(transformedCode);
return '```typescript\n' + transformedCode + '\n```';
} catch (error) {
console.warn(`Failed to transform code block: ${error}\n\n${codeBlock}`);
return match; // Return original code block if transformation fails
}
},
);

fs.writeFileSync(file, readmeContent);
} catch (error) {
console.error('Error processing README:', error);
throw error;
}
}

const config = {
npm: '@valtown/sdk',
jsr: '@valtown/sdk',
};

processReadme(config, process.argv[2]);
13 changes: 13 additions & 0 deletions scripts/utils/git-swap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
set -exuo pipefail
# the package is published to NPM from ./dist
# we want the final file structure for git installs to match the npm installs, so we

# delete everything except ./dist and ./node_modules
find . -maxdepth 1 -mindepth 1 ! -name 'dist' ! -name 'node_modules' -exec rm -rf '{}' +

# move everything from ./dist to .
mv dist/* .

# delete the now-empty ./dist
rmdir dist
32 changes: 18 additions & 14 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export abstract class APIClient {
maxRetries = 2,
timeout = 60000, // 1 minute
httpAgent,
fetch: overridenFetch,
fetch: overriddenFetch,
}: {
baseURL: string;
maxRetries?: number | undefined;
Expand All @@ -176,7 +176,7 @@ export abstract class APIClient {
this.timeout = validatePositiveInteger('timeout', timeout);
this.httpAgent = httpAgent;

this.fetch = overridenFetch ?? fetch;
this.fetch = overriddenFetch ?? fetch;
}

protected authHeaders(opts: FinalRequestOptions): Headers {
Expand Down Expand Up @@ -522,20 +522,24 @@ export abstract class APIClient {

const timeout = setTimeout(() => controller.abort(), ms);

const fetchOptions = {
signal: controller.signal as any,
...options,
};
if (fetchOptions.method) {
// Custom methods like 'patch' need to be uppercased
// See https://github.com/nodejs/undici/issues/2294
fetchOptions.method = fetchOptions.method.toUpperCase();
}

return (
this.getRequestClient()
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
.fetch.call(undefined, url, { signal: controller.signal as any, ...options })
.finally(() => {
clearTimeout(timeout);
})
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
this.fetch.call(undefined, url, fetchOptions).finally(() => {
clearTimeout(timeout);
})
);
}

protected getRequestClient(): RequestClient {
return { fetch: this.fetch };
}

private shouldRetry(response: Response): boolean {
// Note this is not a standard header.
const shouldRetryHeader = response.headers.get('x-should-retry');
Expand Down Expand Up @@ -976,8 +980,8 @@ export const safeJSON = (text: string) => {
}
};

// https://stackoverflow.com/a/19709846
const startsWithSchemeRegexp = new RegExp('^(?:[a-z]+:)?//', 'i');
// https://url.spec.whatwg.org/#url-scheme-string
const startsWithSchemeRegexp = /^[a-z][a-z0-9+.-]*:/i;
const isAbsoluteURL = (url: string): boolean => {
return startsWithSchemeRegexp.test(url);
};
Expand Down
Loading

0 comments on commit 3ea414f

Please sign in to comment.