Skip to content

Commit d14ee3e

Browse files
scalarbotmarclave
authored andcommitted
1 parent 0b74e80 commit d14ee3e

File tree

288 files changed

+27532
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

288 files changed

+27532
-2
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This allows generated code to be indexed correctly
2+
*.ts linguist-generated=false

.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/models
2+
/models/errors
3+
/types
4+
/node_modules
5+
/lib
6+
/sdk
7+
/funcs
8+
/react-query
9+
/mcp-server
10+
/hooks
11+
/index.*
12+
/core.*
13+
/bin
14+
/cjs
15+
/esm
16+
/dist
17+
/.tsbuildinfo
18+
/.eslintcache
19+
/.tshy
20+
/.tshy-*
21+
/__tests__
22+
.DS_Store
23+
**/.speakeasy/temp/
24+
**/.speakeasy/logs/
25+
.DS_Store
26+
/.speakeasy/reports

.npmignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
**/*
2+
!/FUNCTIONS.md
3+
!/RUNTIMES.md
4+
!/REACT_QUERY.md
5+
!/**/*.ts
6+
!/**/*.js
7+
!/**/*.mjs
8+
!/**/*.json
9+
!/**/*.map
10+
11+
/eslint.config.mjs
12+
/cjs
13+
/.tshy
14+
/.tshy-*
15+
/__tests__

CONTRIBUTING.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Contributing to This Repository
2+
3+
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.
4+
5+
## How to Report Issues
6+
7+
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:
8+
9+
- A clear and descriptive title
10+
- Steps to reproduce the issue
11+
- Expected and actual behavior
12+
- Any relevant logs, screenshots, or error messages
13+
- Information about your environment (e.g., operating system, software versions)
14+
- For example can be collected using the `npx envinfo` command from your terminal if you have Node.js installed
15+
16+
## Issue Triage and Upstream Fixes
17+
18+
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.
19+
20+
## Contact
21+
22+
If you have any questions or need further assistance, please feel free to reach out by opening an issue.
23+
24+
Thank you for your understanding and cooperation!
25+
26+
The Maintainers

FUNCTIONS.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Standalone Functions
2+
3+
> [!NOTE]
4+
> This section is useful if you are using a bundler and targetting browsers and
5+
> runtimes where the size of an application affects performance and load times.
6+
7+
Every method in this SDK is also available as a standalone function. This
8+
alternative API is suitable when targetting the browser or serverless runtimes
9+
and using a bundler to build your application since all unused functionality
10+
will be tree-shaken away. This includes code for unused methods, Zod schemas,
11+
encoding helpers and response handlers. The result is dramatically smaller
12+
impact on the application's final bundle size which grows very slowly as you use
13+
more and more functionality from this SDK.
14+
15+
Calling methods through the main SDK class remains a valid and generally more
16+
more ergonomic option. Standalone functions represent an optimisation for a
17+
specific category of applications.
18+
19+
## Example
20+
21+
```typescript
22+
import { ScalarCore } from "@scalar/sdk/core.js";
23+
import { apiDocsGetv1ApisNamespace } from "@scalar/sdk/funcs/apiDocsGetv1ApisNamespace.js";
24+
import { SDKValidationError } from "@scalar/sdk/models/errors/sdkvalidationerror.js";
25+
26+
// Use `ScalarCore` for best tree-shaking performance.
27+
// You can create one instance of it to use across an application.
28+
const scalar = new ScalarCore({
29+
bearerAuth: "<YOUR_BEARER_TOKEN_HERE>",
30+
});
31+
32+
async function run() {
33+
const res = await apiDocsGetv1ApisNamespace(scalar, {
34+
namespace: "<value>",
35+
});
36+
37+
switch (true) {
38+
case res.ok:
39+
// The success case will be handled outside of the switch block
40+
break;
41+
case res.error instanceof SDKValidationError:
42+
// Pretty-print validation errors.
43+
return console.log(res.error.pretty());
44+
case res.error instanceof Error:
45+
return console.log(res.error);
46+
default:
47+
// TypeScript's type checking will fail on the following line if the above
48+
// cases were not exhaustive.
49+
res.error satisfies never;
50+
throw new Error("Assertion failed: expected error checks to be exhaustive: " + res.error);
51+
}
52+
53+
54+
const { value: result } = res;
55+
56+
// Handle the result
57+
console.log(result);
58+
}
59+
60+
run();
61+
```
62+
63+
## Result types
64+
65+
Standalone functions differ from SDK methods in that they return a
66+
`Result<Value, Error>` type to capture _known errors_ and document them using
67+
the type system. By avoiding throwing errors, application code maintains clear
68+
control flow and error-handling become part of the regular flow of application
69+
code.
70+
71+
> We use the term "known errors" because standalone functions, and JavaScript
72+
> code in general, can still throw unexpected errors such as `TypeError`s,
73+
> `RangeError`s and `DOMException`s. Exhaustively catching all errors may be
74+
> something this SDK addresses in the future. Nevertheless, there is still a lot
75+
> of benefit from capturing most errors and turning them into values.
76+
77+
The second reason for this style of programming is because these functions will
78+
typically be used in front-end applications where exception throwing is
79+
sometimes discouraged or considered unidiomatic. React and similar ecosystems
80+
and libraries tend to promote this style of programming so that components
81+
render useful content under all states (loading, success, error and so on).
82+
83+
The general pattern when calling standalone functions looks like this:
84+
85+
```typescript
86+
import { Core } from "<sdk-package-name>";
87+
import { fetchSomething } from "<sdk-package-name>/funcs/fetchSomething.js";
88+
89+
const client = new Core();
90+
91+
async function run() {
92+
const result = await fetchSomething(client, { id: "123" });
93+
if (!result.ok) {
94+
// You can throw the error or handle it. It's your choice now.
95+
throw result.error;
96+
}
97+
98+
console.log(result.value);
99+
}
100+
101+
run();
102+
```
103+
104+
Notably, `result.error` above will have an explicit type compared to a try-catch
105+
variation where the error in the catch block can only be of type `unknown` (or
106+
`any` depending on your TypeScript settings).

0 commit comments

Comments
 (0)