Skip to content

Commit

Permalink
feat: push up sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-timothy-albert committed Nov 19, 2024
0 parents commit 21a8d21
Show file tree
Hide file tree
Showing 80 changed files with 11,335 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-env node */
module.exports = {
root: true,
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/recommended",
"plugin:import/typescript",
],
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
settings: {
"import/resolver": {
typescript: true,
node: true,
},
},
rules: {
// Handled by typescript compiler
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/no-explicit-any": "off",
"import/no-named-as-default-member": "off",

"import/no-default-export": "error",
},
};
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This allows generated code to be indexed correctly
*.ts linguist-generated=false
30 changes: 30 additions & 0 deletions .github/workflows/sdk_generation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Generate
permissions:
checks: write
contents: write
pull-requests: write
statuses: write
id-token: write
"on":
workflow_dispatch:
inputs:
force:
description: Force generation of SDKs
type: boolean
default: false
set_version:
description: optionally set a specific SDK version
type: string
schedule:
- cron: 0 0 * * *
jobs:
generate:
uses: speakeasy-api/sdk-generation-action/.github/workflows/workflow-executor.yaml@v15
with:
force: ${{ github.event.inputs.force }}
mode: pr
set_version: ${{ github.event.inputs.set_version }}
secrets:
github_access_token: ${{ secrets.GITHUB_TOKEN }}
npm_token: ${{ secrets.NPM_TOKEN }}
speakeasy_api_key: ${{ secrets.SPEAKEASY_API_KEY }}
23 changes: 23 additions & 0 deletions .github/workflows/sdk_publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Publish
permissions:
checks: write
contents: write
pull-requests: write
statuses: write
id-token: write
"on":
push:
branches:
- main
paths:
- .speakeasy/gen.lock
workflow_dispatch: {}
jobs:
publish:
uses: speakeasy-api/sdk-generation-action/.github/workflows/sdk-publish.yaml@v15
with:
target: code_samples_typescript_sdk
secrets:
github_access_token: ${{ secrets.GITHUB_TOKEN }}
npm_token: ${{ secrets.NPM_TOKEN }}
speakeasy_api_key: ${{ secrets.SPEAKEASY_API_KEY }}
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/models
/models/errors
/types
/node_modules
/lib
/sdk
/funcs
/hooks
/index.*
/core.*
/cjs
/esm
/dist
/.tsbuildinfo
/.tshy
/.tshy-*
/__tests__
10 changes: 10 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
**/*
!/**/*.ts
!/**/*.js
!/**/*.map

/.eslintrc.js
/cjs
/.tshy
/.tshy-*
/__tests__
44 changes: 44 additions & 0 deletions .speakeasy/gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
configVersion: 2.0.0
generation:
sdkClassName: SDK
maintainOpenAPIOrder: true
usageSnippets:
optionalPropertyRendering: withExample
useClassNamesForArrayFields: true
fixes:
nameResolutionDec2023: true
parameterOrderingFeb2024: true
requestResponseComponentNamesFeb2024: true
auth:
oAuth2ClientCredentialsEnabled: false
oAuth2PasswordEnabled: false
typescript:
version: 0.35.0
additionalDependencies:
dependencies: {}
devDependencies: {}
peerDependencies: {}
additionalPackageJSON: {}
author: Speakeasy
clientServerStatusCodesAsErrors: true
defaultErrorName: APIError
enumFormat: enum
flattenGlobalSecurity: true
flatteningOrder: body-first
imports:
option: openapi
paths:
callbacks: models/callbacks
errors: models/errors
operations: models/operations
shared: models/components
webhooks: models/webhooks
inputModelSuffix: input
maxMethodParams: 1
methodArguments: require-security-and-request
moduleFormat: commonjs
outputModelSuffix: output
packageName: '@speakeasy-api/code-samples'
responseFormat: flat
templateVersion: v2
useIndexModules: true
33 changes: 33 additions & 0 deletions .speakeasy/workflow.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
speakeasyVersion: 1.441.0
sources:
speakeasy-OAS:
sourceNamespace: speakeasy-oas
sourceRevisionDigest: sha256:eed9083bc4ba2cf34738402866ce9bded44a4172874f2728d988479367ca0096
sourceBlobDigest: sha256:0d8cab250914be6d6f0c159e5f64c6690143fb49dce89117d06c3635bdc784b0
tags:
- latest
targets:
code_samples_typescript_sdk:
source: speakeasy-OAS
sourceNamespace: speakeasy-oas
sourceRevisionDigest: sha256:eed9083bc4ba2cf34738402866ce9bded44a4172874f2728d988479367ca0096
sourceBlobDigest: sha256:0d8cab250914be6d6f0c159e5f64c6690143fb49dce89117d06c3635bdc784b0
workflow:
workflowVersion: 1.0.0
speakeasyVersion: latest
sources:
speakeasy-OAS:
inputs:
- location: https://www.speakeasy.com/openapi.yaml
overlays:
- location: overlay.yaml
transformations:
- filterOperations:
operations: generateCodeSamplePreview, generateCodeSamplePreviewAsync, getCodeSamplePreviewAsync
include: true
registry:
location: registry.speakeasyapi.dev/speakeasy-self/speakeasy-self/speakeasy-oas
targets:
code_samples_typescript_sdk:
target: typescript
source: speakeasy-OAS
21 changes: 21 additions & 0 deletions .speakeasy/workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
workflowVersion: 1.0.0
speakeasyVersion: latest
sources:
speakeasy-OAS:
inputs:
- location: https://www.speakeasy.com/openapi.yaml
overlays:
- location: overlay.yaml
transformations:
- filterOperations:
operations: generateCodeSamplePreview, generateCodeSamplePreviewAsync, getCodeSamplePreviewAsync
include: true
registry:
location: registry.speakeasyapi.dev/speakeasy-self/speakeasy-self/speakeasy-oas
targets:
code_samples_typescript_sdk:
target: typescript
source: speakeasy-OAS
# publish:
# npm:
# token: $npm_token
26 changes: 26 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Contributing to This Repository

Thank you for your interest in contributing to this repository. Please note that this repository contains generated code. As such, we do not accept direct changes or pull requests. Instead, we encourage you to follow the guidelines below to report issues and suggest improvements.

## How to Report Issues

If you encounter any bugs or have suggestions for improvements, please open an issue on GitHub. When reporting an issue, please provide as much detail as possible to help us reproduce the problem. This includes:

- A clear and descriptive title
- Steps to reproduce the issue
- Expected and actual behavior
- Any relevant logs, screenshots, or error messages
- Information about your environment (e.g., operating system, software versions)
- For example can be collected using the `npx envinfo` command from your terminal if you have Node.js installed

## Issue Triage and Upstream Fixes

We will review and triage issues as quickly as possible. Our goal is to address bugs and incorporate improvements in the upstream source code. Fixes will be included in the next generation of the generated code.

## Contact

If you have any questions or need further assistance, please feel free to reach out by opening an issue.

Thank you for your understanding and cooperation!

The Maintainers
113 changes: 113 additions & 0 deletions FUNCTIONS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Standalone Functions

> [!NOTE]
> This section is useful if you are using a bundler and targetting browsers and
> runtimes where the size of an application affects performance and load times.
Every method in this SDK is also available as a standalone function. This
alternative API is suitable when targetting the browser or serverless runtimes
and using a bundler to build your application since all unused functionality
will be tree-shaken away. This includes code for unused methods, Zod schemas,
encoding helpers and response handlers. The result is dramatically smaller
impact on the application's final bundle size which grows very slowly as you use
more and more functionality from this SDK.

Calling methods through the main SDK class remains a valid and generally more
more ergonomic option. Standalone functions represent an optimisation for a
specific category of applications.

## Example

```typescript
import { SDKCore } from "@speakeasy-api/code-samples/core.js";
import { codesamplesPreview } from "@speakeasy-api/code-samples/funcs/codesamplesPreview.js";
import { SDKValidationError } from "@speakeasy-api/code-samples/models/errors/sdkvalidationerror.js";
import { openAsBlob } from "node:fs";

// Use `SDKCore` for best tree-shaking performance.
// You can create one instance of it to use across an application.
const sdk = new SDKCore({
security: {
apiKey: "<YOUR_API_KEY_HERE>",
},
});

async function run() {
const res = await codesamplesPreview(sdk, {
languages: [
"<value>",
"<value>",
],
schemaFile: await openAsBlob("example.file"),
});

switch (true) {
case res.ok:
// The success case will be handled outside of the switch block
break;
case res.error instanceof SDKValidationError:
// Pretty-print validation errors.
return console.log(res.error.pretty());
case res.error instanceof Error:
return console.log(res.error);
default:
// TypeScript's type checking will fail on the following line if the above
// cases were not exhaustive.
res.error satisfies never;
throw new Error("Assertion failed: expected error checks to be exhaustive: " + res.error);
}


const { value: result } = res;

// Handle the result
console.log(result);
}

run();
```

## Result types

Standalone functions differ from SDK methods in that they return a
`Result<Value, Error>` type to capture _known errors_ and document them using
the type system. By avoiding throwing errors, application code maintains clear
control flow and error-handling become part of the regular flow of application
code.

> We use the term "known errors" because standalone functions, and JavaScript
> code in general, can still throw unexpected errors such as `TypeError`s,
> `RangeError`s and `DOMException`s. Exhaustively catching all errors may be
> something this SDK addresses in the future. Nevertheless, there is still a lot
> of benefit from capturing most errors and turning them into values.
The second reason for this style of programming is because these functions will
typically be used in front-end applications where exception throwing is
sometimes discouraged or considered unidiomatic. React and similar ecosystems
and libraries tend to promote this style of programming so that components
render useful content under all states (loading, success, error and so on).

The general pattern when calling standalone functions looks like this:

```typescript
import { Core } from "<sdk-package-name>";
import { fetchSomething } from "<sdk-package-name>/funcs/fetchSomething.js";

const client = new Core();

async function run() {
const result = await fetchSomething(client, { id: "123" });
if (!result.ok) {
// You can throw the error or handle it. It's your choice now.
throw result.error;
}

console.log(result.value);
}

run();
```

Notably, `result.error` above will have an explicit type compared to a try-catch
variation where the error in the catch block can only be of type `unknown` (or
`any` depending on your TypeScript settings).
Loading

0 comments on commit 21a8d21

Please sign in to comment.