Skip to content

Commit

Permalink
chore: update SDK settings (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] committed Jan 3, 2025
1 parent d5f0059 commit c372d79
Show file tree
Hide file tree
Showing 39 changed files with 137 additions and 87 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/publish-npm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This workflow is triggered when a GitHub release is created.
# It can also be run manually to re-publish to NPM in case it failed for some reason.
# You can run this workflow by navigating to https://www.github.com/vlm-run/vlmrun-node-sdk/actions/workflows/publish-npm.yml
name: Publish NPM
on:
workflow_dispatch:

release:
types: [published]

jobs:
publish:
name: publish
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Node
uses: actions/setup-node@v3
with:
node-version: '18'

- name: Install dependencies
run: |
yarn install
- name: Publish to NPM
run: |
bash ./bin/publish-npm
env:
NPM_TOKEN: ${{ secrets.VLM_NPM_TOKEN || secrets.NPM_TOKEN }}
1 change: 1 addition & 0 deletions .github/workflows/release-doctor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ jobs:
run: |
bash ./bin/check-release-environment
env:
NPM_TOKEN: ${{ secrets.VLM_NPM_TOKEN || secrets.NPM_TOKEN }}

18 changes: 16 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ $ cd vlmrun-node-sdk
# With yarn
$ yarn link
$ cd ../my-package
$ yarn link vlm
$ yarn link vlmrun

# With pnpm
$ pnpm link --global
$ cd ../my-package
$ pnpm link -—global vlm
$ pnpm link -—global vlmrun
```

## Running tests
Expand Down Expand Up @@ -91,3 +91,17 @@ To format and fix all lint issues automatically:
```sh
$ yarn fix
```

## Publishing and releases

Changes made to this repository via the automated release PR pipeline should publish to npm automatically. If
the changes aren't made through the automated pipeline, you may want to make releases manually.

### Publish with a GitHub workflow

You can release to package managers by using [the `Publish NPM` GitHub action](https://www.github.com/vlm-run/vlmrun-node-sdk/actions/workflows/publish-npm.yml). This requires a setup organization or repository secret to be set up.

### Publish manually

If you need to manually release a package, you can run the `bin/publish-npm` script with an `NPM_TOKEN` set on
the environment.
19 changes: 8 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Vlm Node API Library

[![NPM version](https://img.shields.io/npm/v/vlm.svg)](https://npmjs.org/package/vlm) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/vlm)
[![NPM version](https://img.shields.io/npm/v/vlmrun.svg)](https://npmjs.org/package/vlmrun) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/vlmrun)

This library provides convenient access to the Vlm REST API from server-side TypeScript or JavaScript.

Expand All @@ -11,19 +11,16 @@ It is generated with [Stainless](https://www.stainlessapi.com/).
## Installation

```sh
npm install git+ssh://[email protected]:vlm-run/vlmrun-node-sdk.git
npm install vlmrun
```

> [!NOTE]
> Once this package is [published to npm](https://app.stainlessapi.com/docs/guides/publish), this will become: `npm install vlm`
## Usage

The full API of this library can be found in [api.md](api.md).

<!-- prettier-ignore -->
```js
import Vlm from 'vlm';
import Vlm from 'vlmrun';

const client = new Vlm();

Expand All @@ -42,7 +39,7 @@ This library includes TypeScript definitions for all request params and response

<!-- prettier-ignore -->
```ts
import Vlm from 'vlm';
import Vlm from 'vlmrun';

const client = new Vlm();

Expand Down Expand Up @@ -212,11 +209,11 @@ add the following import before your first import `from "Vlm"`:
```ts
// Tell TypeScript and the package to use the global web fetch instead of node-fetch.
// Note, despite the name, this does not add any polyfills, but expects them to be provided if needed.
import 'vlm/shims/web';
import Vlm from 'vlm';
import 'vlmrun/shims/web';
import Vlm from 'vlmrun';
```

To do the inverse, add `import "vlm/shims/node"` (which does import polyfills).
To do the inverse, add `import "vlmrun/shims/node"` (which does import polyfills).
This can also be useful if you are getting the wrong TypeScript types for `Response` ([more details](https://github.com/vlm-run/vlmrun-node-sdk/tree/main/src/_shims#readme)).

### Logging and middleware
Expand All @@ -226,7 +223,7 @@ which can be used to inspect or alter the `Request` or `Response` before/after e

```ts
import { fetch } from 'undici'; // as one example
import Vlm from 'vlm';
import Vlm from 'vlmrun';

const client = new Vlm({
fetch: async (url: RequestInfo, init?: RequestInit): Promise<Response> => {
Expand Down
4 changes: 4 additions & 0 deletions bin/check-release-environment
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

errors=()

if [ -z "${NPM_TOKEN}" ]; then
errors+=("The VLM_NPM_TOKEN secret has not been set. Please set it in either this repository's secrets or your organization secrets")
fi

lenErrors=${#errors[@]}

if [[ lenErrors -gt 0 ]]; then
Expand Down
6 changes: 3 additions & 3 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const config: JestConfigWithTsJest = {
'^.+\\.(t|j)sx?$': ['@swc/jest', { sourceMaps: 'inline' }],
},
moduleNameMapper: {
'^vlm$': '<rootDir>/src/index.ts',
'^vlm/_shims/auto/(.*)$': '<rootDir>/src/_shims/auto/$1-node',
'^vlm/(.*)$': '<rootDir>/src/$1',
'^vlmrun$': '<rootDir>/src/index.ts',
'^vlmrun/_shims/auto/(.*)$': '<rootDir>/src/_shims/auto/$1-node',
'^vlmrun/(.*)$': '<rootDir>/src/$1',
},
modulePathIgnorePatterns: [
'<rootDir>/ecosystem-tests/',
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "vlm",
"name": "vlmrun",
"version": "0.0.1-alpha.0",
"description": "The official TypeScript library for the Vlm API",
"author": "Vlm <[email protected]>",
Expand Down Expand Up @@ -59,8 +59,8 @@
"./shims/web.mjs"
],
"imports": {
"vlm": ".",
"vlm/*": "./src/*"
"vlmrun": ".",
"vlmrun/*": "./src/*"
},
"exports": {
"./_shims/auto/*": {
Expand Down
6 changes: 3 additions & 3 deletions scripts/build
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ node scripts/utils/check-version.cjs

# Build into dist and will publish the package from there,
# so that src/resources/foo.ts becomes <package root>/resources/foo.js
# This way importing from `"vlm/resources/foo"` works
# This way importing from `"vlmrun/resources/foo"` works
# even with `"moduleResolution": "node"`

rm -rf dist; mkdir dist
Expand Down Expand Up @@ -47,8 +47,8 @@ node scripts/utils/postprocess-files.cjs

# make sure that nothing crashes when we require the output CJS or
# import the output ESM
(cd dist && node -e 'require("vlm")')
(cd dist && node -e 'import("vlm")' --input-type=module)
(cd dist && node -e 'require("vlmrun")')
(cd dist && node -e 'import("vlmrun")' --input-type=module)

if [ -e ./scripts/build-deno ]
then
Expand Down
4 changes: 2 additions & 2 deletions scripts/utils/postprocess-files.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require('fs');
const path = require('path');
const { parse } = require('@typescript-eslint/parser');

const pkgImportPath = process.env['PKG_IMPORT_PATH'] ?? 'vlm/';
const pkgImportPath = process.env['PKG_IMPORT_PATH'] ?? 'vlmrun/';

const distDir =
process.env['DIST_PATH'] ?
Expand Down Expand Up @@ -142,7 +142,7 @@ async function postprocess() {

if (file.endsWith('.d.ts')) {
// work around bad tsc behavior
// if we have `import { type Readable } from 'vlm/_shims/index'`,
// if we have `import { type Readable } from 'vlmrun/_shims/index'`,
// tsc sometimes replaces `Readable` with `import("stream").Readable` inline
// in the output .d.ts
transformed = transformed.replace(/import\("stream"\).Readable/g, 'Readable');
Expand Down
32 changes: 16 additions & 16 deletions src/_shims/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# 👋 Wondering what everything in here does?

`vlm` supports a wide variety of runtime environments like Node.js, Deno, Bun, browsers, and various
`vlmrun` supports a wide variety of runtime environments like Node.js, Deno, Bun, browsers, and various
edge runtimes, as well as both CommonJS (CJS) and EcmaScript Modules (ESM).

To do this, `vlm` provides shims for either using `node-fetch` when in Node (because `fetch` is still experimental there) or the global `fetch` API built into the environment when not in Node.
To do this, `vlmrun` provides shims for either using `node-fetch` when in Node (because `fetch` is still experimental there) or the global `fetch` API built into the environment when not in Node.

It uses [conditional exports](https://nodejs.org/api/packages.html#conditional-exports) to
automatically select the correct shims for each environment. However, conditional exports are a fairly new
Expand All @@ -15,32 +15,32 @@ getting the wrong raw `Response` type from `.asResponse()`, for example.

The user can work around these issues by manually importing one of:

- `import 'vlm/shims/node'`
- `import 'vlm/shims/web'`
- `import 'vlmrun/shims/node'`
- `import 'vlmrun/shims/web'`

All of the code here in `_shims` handles selecting the automatic default shims or manual overrides.

### How it works - Runtime

Runtime shims get installed by calling `setShims` exported by `vlm/_shims/registry`.
Runtime shims get installed by calling `setShims` exported by `vlmrun/_shims/registry`.

Manually importing `vlm/shims/node` or `vlm/shims/web`, calls `setShims` with the respective runtime shims.
Manually importing `vlmrun/shims/node` or `vlmrun/shims/web`, calls `setShims` with the respective runtime shims.

All client code imports shims from `vlm/_shims/index`, which:
All client code imports shims from `vlmrun/_shims/index`, which:

- checks if shims have been set manually
- if not, calls `setShims` with the shims from `vlm/_shims/auto/runtime`
- re-exports the installed shims from `vlm/_shims/registry`.
- if not, calls `setShims` with the shims from `vlmrun/_shims/auto/runtime`
- re-exports the installed shims from `vlmrun/_shims/registry`.

`vlm/_shims/auto/runtime` exports web runtime shims.
If the `node` export condition is set, the export map replaces it with `vlm/_shims/auto/runtime-node`.
`vlmrun/_shims/auto/runtime` exports web runtime shims.
If the `node` export condition is set, the export map replaces it with `vlmrun/_shims/auto/runtime-node`.

### How it works - Type time

All client code imports shim types from `vlm/_shims/index`, which selects the manual types from `vlm/_shims/manual-types` if they have been declared, otherwise it exports the auto types from `vlm/_shims/auto/types`.
All client code imports shim types from `vlmrun/_shims/index`, which selects the manual types from `vlmrun/_shims/manual-types` if they have been declared, otherwise it exports the auto types from `vlmrun/_shims/auto/types`.

`vlm/_shims/manual-types` exports an empty namespace.
Manually importing `vlm/shims/node` or `vlm/shims/web` merges declarations into this empty namespace, so they get picked up by `vlm/_shims/index`.
`vlmrun/_shims/manual-types` exports an empty namespace.
Manually importing `vlmrun/shims/node` or `vlmrun/shims/web` merges declarations into this empty namespace, so they get picked up by `vlmrun/_shims/index`.

`vlm/_shims/auto/types` exports web type definitions.
If the `node` export condition is set, the export map replaces it with `vlm/_shims/auto/types-node`, though TS only picks this up if `"moduleResolution": "nodenext"` or `"moduleResolution": "bundler"`.
`vlmrun/_shims/auto/types` exports web type definitions.
If the `node` export condition is set, the export map replaces it with `vlmrun/_shims/auto/types-node`, though TS only picks this up if `"moduleResolution": "nodenext"` or `"moduleResolution": "bundler"`.
2 changes: 1 addition & 1 deletion src/_shims/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
*/
import { manual } from './manual-types';
import * as auto from 'vlm/_shims/auto/types';
import * as auto from 'vlmrun/_shims/auto/types';
import { type RequestOptions } from '../core';

type SelectType<Manual, Auto> = unknown extends Manual ? Auto : Manual;
Expand Down
2 changes: 1 addition & 1 deletion src/_shims/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
*/
const shims = require('./registry');
const auto = require('vlm/_shims/auto/runtime');
const auto = require('vlmrun/_shims/auto/runtime');
if (!shims.kind) shims.setShims(auto.getRuntime(), { auto: true });
for (const property of Object.keys(shims)) {
Object.defineProperty(exports, property, {
Expand Down
2 changes: 1 addition & 1 deletion src/_shims/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
*/
import * as shims from './registry.mjs';
import * as auto from 'vlm/_shims/auto/runtime';
import * as auto from 'vlmrun/_shims/auto/runtime';
if (!shims.kind) shims.setShims(auto.getRuntime(), { auto: true });
export * from './registry.mjs';
4 changes: 2 additions & 2 deletions src/_shims/manual-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
/**
* Types will get added to this namespace when you import one of the following:
*
* import 'vlm/shims/node'
* import 'vlm/shims/web'
* import 'vlmrun/shims/node'
* import 'vlmrun/shims/web'
*
* Importing more than one will cause type and runtime errors.
*/
Expand Down
6 changes: 4 additions & 2 deletions src/_shims/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ export let isFsReadStream: Shims['isFsReadStream'] | undefined = undefined;

export function setShims(shims: Shims, options: { auto: boolean } = { auto: false }) {
if (auto) {
throw new Error(`you must \`import 'vlm/shims/${shims.kind}'\` before importing anything else from vlm`);
throw new Error(
`you must \`import 'vlmrun/shims/${shims.kind}'\` before importing anything else from vlmrun`,
);
}
if (kind) {
throw new Error(`can't \`import 'vlm/shims/${shims.kind}'\` after \`import 'vlm/shims/${kind}'\``);
throw new Error(`can't \`import 'vlmrun/shims/${shims.kind}'\` after \`import 'vlmrun/shims/${kind}'\``);
}
auto = options.auto;
kind = shims.kind;
Expand Down
6 changes: 3 additions & 3 deletions src/_shims/web-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export function getRuntime({ manuallyImported }: { manuallyImported?: boolean }
const recommendation =
manuallyImported ?
`You may need to use polyfills`
: `Add one of these imports before your first \`import … from 'vlm'\`:
- \`import 'vlm/shims/node'\` (if you're running on Node)
- \`import 'vlm/shims/web'\` (otherwise)
: `Add one of these imports before your first \`import … from 'vlmrun'\`:
- \`import 'vlmrun/shims/node'\` (if you're running on Node)
- \`import 'vlmrun/shims/web'\` (otherwise)
`;

let _fetch, _Request, _Response, _Headers;
Expand Down
12 changes: 6 additions & 6 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ export class APIPromise<T> extends Promise<T> {
*
* 👋 Getting the wrong TypeScript type for `Response`?
* Try setting `"moduleResolution": "NodeNext"` if you can,
* or add one of these imports before your first `import … from 'vlm'`:
* - `import 'vlm/shims/node'` (if you're running on Node)
* - `import 'vlm/shims/web'` (otherwise)
* or add one of these imports before your first `import … from 'vlmrun'`:
* - `import 'vlmrun/shims/node'` (if you're running on Node)
* - `import 'vlmrun/shims/web'` (otherwise)
*/
asResponse(): Promise<Response> {
return this.responsePromise.then((p) => p.response);
Expand All @@ -115,9 +115,9 @@ export class APIPromise<T> extends Promise<T> {
*
* 👋 Getting the wrong TypeScript type for `Response`?
* Try setting `"moduleResolution": "NodeNext"` if you can,
* or add one of these imports before your first `import … from 'vlm'`:
* - `import 'vlm/shims/node'` (if you're running on Node)
* - `import 'vlm/shims/web'` (otherwise)
* or add one of these imports before your first `import … from 'vlmrun'`:
* - `import 'vlmrun/shims/node'` (if you're running on Node)
* - `import 'vlmrun/shims/web'` (otherwise)
*/
async withResponse(): Promise<{ data: T; response: Response }> {
const [data, response] = await Promise.all([this.parse(), this.asResponse()]);
Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/audio.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

import Vlm from 'vlm';
import Vlm from 'vlmrun';
import { Response } from 'node-fetch';

const client = new Vlm({
Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/document.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

import Vlm from 'vlm';
import Vlm from 'vlmrun';
import { Response } from 'node-fetch';

const client = new Vlm({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

import Vlm from 'vlm';
import Vlm from 'vlmrun';
import { Response } from 'node-fetch';

const client = new Vlm({
Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/experimental/experimental.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

import Vlm from 'vlm';
import Vlm from 'vlmrun';
import { Response } from 'node-fetch';

const client = new Vlm({
Expand Down
Loading

0 comments on commit c372d79

Please sign in to comment.