Skip to content

Commit

Permalink
feat(api): update via SDK Studio (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and stainless-bot committed Jun 13, 2024
1 parent 01cddec commit e90c408
Show file tree
Hide file tree
Showing 35 changed files with 797 additions and 251 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ cd typescript-sdk
# With yarn
yarn link
cd ../my-package
yarn link @@@@prompt-foundry/typescript-sdk
yarn link @prompt-foundry/typescript-sdk

# With pnpm
pnpm link --global
cd ../my-package
pnpm link -—global @@@@prompt-foundry/typescript-sdk
pnpm link -—global @prompt-foundry/typescript-sdk
```

## Running tests
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Prompt Foundry Node API Library

[![NPM version](https://img.shields.io/npm/v/@@@@prompt-foundry/typescript-sdk.svg)](https://npmjs.org/package/@@@@prompt-foundry/typescript-sdk) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/@@@@prompt-foundry/typescript-sdk)
[![NPM version](https://img.shields.io/npm/v/@prompt-foundry/typescript-sdk.svg)](https://npmjs.org/package/@prompt-foundry/typescript-sdk) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/@prompt-foundry/typescript-sdk)

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

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

```sh
npm install @@@@prompt-foundry/typescript-sdk
npm install @prompt-foundry/typescript-sdk
```

## Usage
Expand All @@ -20,7 +20,7 @@ The full API of this library can be found in [api.md](api.md).

<!-- prettier-ignore -->
```js
import PromptFoundry from '@@@@prompt-foundry/typescript-sdk';
import PromptFoundry from '@prompt-foundry/typescript-sdk';

const promptFoundry = new PromptFoundry({
apiKey: process.env['PROMPT_FOUNDRY_API_KEY'], // This is the default and can be omitted
Expand All @@ -41,7 +41,7 @@ This library includes TypeScript definitions for all request params and response

<!-- prettier-ignore -->
```ts
import PromptFoundry from '@@@@prompt-foundry/typescript-sdk';
import PromptFoundry from '@prompt-foundry/typescript-sdk';

const promptFoundry = new PromptFoundry({
apiKey: process.env['PROMPT_FOUNDRY_API_KEY'], // This is the default and can be omitted
Expand Down Expand Up @@ -212,11 +212,11 @@ add the following import before your first import `from "PromptFoundry"`:
```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 '@@@@prompt-foundry/typescript-sdk/shims/web';
import PromptFoundry from '@@@@prompt-foundry/typescript-sdk';
import '@prompt-foundry/typescript-sdk/shims/web';
import PromptFoundry from '@prompt-foundry/typescript-sdk';
```

To do the inverse, add `import "@@@@prompt-foundry/typescript-sdk/shims/node"` (which does import polyfills).
To do the inverse, add `import "@prompt-foundry/typescript-sdk/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/prompt-foundry/typescript-sdk/tree/main/src/_shims#readme)).

### Logging and middleware
Expand All @@ -226,7 +226,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 PromptFoundry from '@@@@prompt-foundry/typescript-sdk';
import PromptFoundry from '@prompt-foundry/typescript-sdk';

const client = new PromptFoundry({
fetch: async (url: RequestInfo, init?: RequestInit): Promise<Response> => {
Expand Down Expand Up @@ -282,7 +282,7 @@ TypeScript >= 4.5 is supported.
The following runtimes are supported:

- Node.js 18 LTS or later ([non-EOL](https://endoflife.date/nodejs)) versions.
- Deno v1.28.0 or higher, using `import PromptFoundry from "npm:@@@@prompt-foundry/typescript-sdk"`.
- Deno v1.28.0 or higher, using `import PromptFoundry from "npm:@prompt-foundry/typescript-sdk"`.
- Bun 1.0 or later.
- Cloudflare Workers.
- Vercel Edge Runtime.
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: {
'^@@@@prompt-foundry/typescript-sdk$': '<rootDir>/src/index.ts',
'^@@@@prompt-foundry/typescript-sdk/_shims/auto/(.*)$': '<rootDir>/src/_shims/auto/$1-node',
'^@@@@prompt-foundry/typescript-sdk/(.*)$': '<rootDir>/src/$1',
'^@prompt-foundry/typescript-sdk$': '<rootDir>/src/index.ts',
'^@prompt-foundry/typescript-sdk/_shims/auto/(.*)$': '<rootDir>/src/_shims/auto/$1-node',
'^@prompt-foundry/typescript-sdk/(.*)$': '<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": "@@@@prompt-foundry/typescript-sdk",
"name": "@prompt-foundry/typescript-sdk",
"version": "1.0.0",
"description": "The official TypeScript library for the Prompt Foundry API",
"author": "Prompt Foundry <dev-feedback@promptfoundry.ai>",
Expand Down Expand Up @@ -60,8 +60,8 @@
"./shims/web.mjs"
],
"imports": {
"@@@@prompt-foundry/typescript-sdk": ".",
"@@@@prompt-foundry/typescript-sdk/*": "./src/*"
"@prompt-foundry/typescript-sdk": ".",
"@prompt-foundry/typescript-sdk/*": "./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 `"@@@@prompt-foundry/typescript-sdk/resources/foo"` works
# This way importing from `"@prompt-foundry/typescript-sdk/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("@@@@prompt-foundry/typescript-sdk")')
(cd dist && node -e 'import("@@@@prompt-foundry/typescript-sdk")' --input-type=module)
(cd dist && node -e 'require("@prompt-foundry/typescript-sdk")')
(cd dist && node -e 'import("@prompt-foundry/typescript-sdk")' --input-type=module)

if command -v deno &> /dev/null && [ -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'] ?? '@@@@prompt-foundry/typescript-sdk/';
const pkgImportPath = process.env['PKG_IMPORT_PATH'] ?? '@prompt-foundry/typescript-sdk/';

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 '@@@@prompt-foundry/typescript-sdk/_shims/index'`,
// if we have `import { type Readable } from '@prompt-foundry/typescript-sdk/_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?

`@@@@prompt-foundry/typescript-sdk` supports a wide variety of runtime environments like Node.js, Deno, Bun, browsers, and various
`@prompt-foundry/typescript-sdk` 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, `@@@@prompt-foundry/typescript-sdk` 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, `@prompt-foundry/typescript-sdk` 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 '@@@@prompt-foundry/typescript-sdk/shims/node'`
- `import '@@@@prompt-foundry/typescript-sdk/shims/web'`
- `import '@prompt-foundry/typescript-sdk/shims/node'`
- `import '@prompt-foundry/typescript-sdk/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 `@@@@prompt-foundry/typescript-sdk/_shims/registry`.
Runtime shims get installed by calling `setShims` exported by `@prompt-foundry/typescript-sdk/_shims/registry`.

Manually importing `@@@@prompt-foundry/typescript-sdk/shims/node` or `@@@@prompt-foundry/typescript-sdk/shims/web`, calls `setShims` with the respective runtime shims.
Manually importing `@prompt-foundry/typescript-sdk/shims/node` or `@prompt-foundry/typescript-sdk/shims/web`, calls `setShims` with the respective runtime shims.

All client code imports shims from `@@@@prompt-foundry/typescript-sdk/_shims/index`, which:
All client code imports shims from `@prompt-foundry/typescript-sdk/_shims/index`, which:

- checks if shims have been set manually
- if not, calls `setShims` with the shims from `@@@@prompt-foundry/typescript-sdk/_shims/auto/runtime`
- re-exports the installed shims from `@@@@prompt-foundry/typescript-sdk/_shims/registry`.
- if not, calls `setShims` with the shims from `@prompt-foundry/typescript-sdk/_shims/auto/runtime`
- re-exports the installed shims from `@prompt-foundry/typescript-sdk/_shims/registry`.

`@@@@prompt-foundry/typescript-sdk/_shims/auto/runtime` exports web runtime shims.
If the `node` export condition is set, the export map replaces it with `@@@@prompt-foundry/typescript-sdk/_shims/auto/runtime-node`.
`@prompt-foundry/typescript-sdk/_shims/auto/runtime` exports web runtime shims.
If the `node` export condition is set, the export map replaces it with `@prompt-foundry/typescript-sdk/_shims/auto/runtime-node`.

### How it works - Type time

All client code imports shim types from `@@@@prompt-foundry/typescript-sdk/_shims/index`, which selects the manual types from `@@@@prompt-foundry/typescript-sdk/_shims/manual-types` if they have been declared, otherwise it exports the auto types from `@@@@prompt-foundry/typescript-sdk/_shims/auto/types`.
All client code imports shim types from `@prompt-foundry/typescript-sdk/_shims/index`, which selects the manual types from `@prompt-foundry/typescript-sdk/_shims/manual-types` if they have been declared, otherwise it exports the auto types from `@prompt-foundry/typescript-sdk/_shims/auto/types`.

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

`@@@@prompt-foundry/typescript-sdk/_shims/auto/types` exports web type definitions.
If the `node` export condition is set, the export map replaces it with `@@@@prompt-foundry/typescript-sdk/_shims/auto/types-node`, though TS only picks this up if `"moduleResolution": "nodenext"` or `"moduleResolution": "bundler"`.
`@prompt-foundry/typescript-sdk/_shims/auto/types` exports web type definitions.
If the `node` export condition is set, the export map replaces it with `@prompt-foundry/typescript-sdk/_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 '@@@@prompt-foundry/typescript-sdk/_shims/auto/types';
import * as auto from '@prompt-foundry/typescript-sdk/_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('@@@@prompt-foundry/typescript-sdk/_shims/auto/runtime');
const auto = require('@prompt-foundry/typescript-sdk/_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 '@@@@prompt-foundry/typescript-sdk/_shims/auto/runtime';
import * as auto from '@prompt-foundry/typescript-sdk/_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 '@@@@prompt-foundry/typescript-sdk/shims/node'
* import '@@@@prompt-foundry/typescript-sdk/shims/web'
* import '@prompt-foundry/typescript-sdk/shims/node'
* import '@prompt-foundry/typescript-sdk/shims/web'
*
* Importing more than one will cause type and runtime errors.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/_shims/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,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 '@@@@prompt-foundry/typescript-sdk/shims/${shims.kind}'\` before importing anything else from @@@@prompt-foundry/typescript-sdk`,
`you must \`import '@prompt-foundry/typescript-sdk/shims/${shims.kind}'\` before importing anything else from @prompt-foundry/typescript-sdk`,
);
}
if (kind) {
throw new Error(
`can't \`import '@@@@prompt-foundry/typescript-sdk/shims/${shims.kind}'\` after \`import '@@@@prompt-foundry/typescript-sdk/shims/${kind}'\``,
`can't \`import '@prompt-foundry/typescript-sdk/shims/${shims.kind}'\` after \`import '@prompt-foundry/typescript-sdk/shims/${kind}'\``,
);
}
auto = options.auto;
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 '@@@@prompt-foundry/typescript-sdk'\`:
- \`import '@@@@prompt-foundry/typescript-sdk/shims/node'\` (if you're running on Node)
- \`import '@@@@prompt-foundry/typescript-sdk/shims/web'\` (otherwise)
: `Add one of these imports before your first \`import … from '@prompt-foundry/typescript-sdk'\`:
- \`import '@prompt-foundry/typescript-sdk/shims/node'\` (if you're running on Node)
- \`import '@prompt-foundry/typescript-sdk/shims/web'\` (otherwise)
`;

let _fetch, _Request, _Response, _Headers;
Expand Down
23 changes: 7 additions & 16 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { VERSION } from './version';
;
import {
PromptFoundryError,
APIError,
Expand Down Expand Up @@ -39,8 +38,6 @@ type APIResponseProps = {

async function defaultParseResponse<T>(props: APIResponseProps): Promise<T> {
const { response } = props;
;

// fetch refuses to read the body when the status code is 204.
if (response.status === 204) {
return null as T;
Expand Down Expand Up @@ -100,9 +97,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 '@@@@prompt-foundry/typescript-sdk'`:
* - `import '@@@@prompt-foundry/typescript-sdk/shims/node'` (if you're running on Node)
* - `import '@@@@prompt-foundry/typescript-sdk/shims/web'` (otherwise)
* or add one of these imports before your first `import … from '@prompt-foundry/typescript-sdk'`:
* - `import '@prompt-foundry/typescript-sdk/shims/node'` (if you're running on Node)
* - `import '@prompt-foundry/typescript-sdk/shims/web'` (otherwise)
*/
asResponse(): Promise<Response> {
return this.responsePromise.then((p) => p.response);
Expand All @@ -116,9 +113,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 '@@@@prompt-foundry/typescript-sdk'`:
* - `import '@@@@prompt-foundry/typescript-sdk/shims/node'` (if you're running on Node)
* - `import '@@@@prompt-foundry/typescript-sdk/shims/web'` (otherwise)
* or add one of these imports before your first `import … from '@prompt-foundry/typescript-sdk'`:
* - `import '@prompt-foundry/typescript-sdk/shims/node'` (if you're running on Node)
* - `import '@prompt-foundry/typescript-sdk/shims/web'` (otherwise)
*/
async withResponse(): Promise<{ data: T; response: Response }> {
const [data, response] = await Promise.all([this.parse(), this.asResponse()]);
Expand Down Expand Up @@ -197,7 +194,7 @@ export abstract class APIClient {
Accept: 'application/json',
'Content-Type': 'application/json',
'User-Agent': this.getUserAgent(),
...getPlatformHeaders(),
...getPlatformHeaders(),
...this.authHeaders(opts),
};
}
Expand Down Expand Up @@ -754,11 +751,8 @@ export type RequestOptions<
signal?: AbortSignal | undefined | null;
idempotencyKey?: string;



__binaryRequest?: boolean | undefined;
__binaryResponse?: boolean | undefined;

};

// This is required so that we can determine if a given object matches the RequestOptions
Expand All @@ -778,11 +772,8 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
signal: true,
idempotencyKey: true,



__binaryRequest: true,
__binaryResponse: true,

};

export const isRequestOptions = (obj: unknown): obj is RequestOptions => {
Expand Down
Loading

0 comments on commit e90c408

Please sign in to comment.