Skip to content

Commit f656d70

Browse files
scalarbotmarclave
authored andcommitted
1 parent 771f91c commit f656d70

File tree

85 files changed

+1416
-1312
lines changed

Some content is hidden

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

85 files changed

+1416
-1312
lines changed

FUNCTIONS.md

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ specific category of applications.
2121
```typescript
2222
import { ScalarCore } from "@scalar/sdk/core.js";
2323
import { apiDocsGetv1ApisNamespace } from "@scalar/sdk/funcs/apiDocsGetv1ApisNamespace.js";
24-
import { SDKValidationError } from "@scalar/sdk/models/errors/sdkvalidationerror.js";
2524

2625
// Use `ScalarCore` for best tree-shaking performance.
2726
// You can create one instance of it to use across an application.
@@ -33,28 +32,12 @@ async function run() {
3332
const res = await apiDocsGetv1ApisNamespace(scalar, {
3433
namespace: "<value>",
3534
});
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);
35+
if (res.ok) {
36+
const { value: result } = res;
37+
console.log(result);
38+
} else {
39+
console.log("apiDocsGetv1ApisNamespace failed:", res.error);
5140
}
52-
53-
54-
const { value: result } = res;
55-
56-
// Handle the result
57-
console.log(result);
5841
}
5942

6043
run();

README.md

Lines changed: 52 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,30 @@ Scalar API: Public facing api to manage all scalar platform entities
4040
<!-- Start SDK Installation [installation] -->
4141
## SDK Installation
4242

43-
> [!TIP]
44-
> To finish publishing your SDK to npm and others you must [run your first generation action](https://www.speakeasy.com/docs/github-setup#step-by-step-guide).
45-
46-
4743
The SDK can be installed with either [npm](https://www.npmjs.com/), [pnpm](https://pnpm.io/), [bun](https://bun.sh/) or [yarn](https://classic.yarnpkg.com/en/) package managers.
4844

4945
### NPM
5046

5147
```bash
52-
npm add <UNSET>
48+
npm add @scalar/sdk
5349
```
5450

5551
### PNPM
5652

5753
```bash
58-
pnpm add <UNSET>
54+
pnpm add @scalar/sdk
5955
```
6056

6157
### Bun
6258

6359
```bash
64-
bun add <UNSET>
60+
bun add @scalar/sdk
6561
```
6662

6763
### Yarn
6864

6965
```bash
70-
yarn add <UNSET> zod
66+
yarn add @scalar/sdk zod
7167

7268
# Note that Yarn does not install peer dependencies automatically. You will need
7369
# to install zod as shown above.
@@ -184,7 +180,6 @@ async function run() {
184180
namespace: "<value>",
185181
});
186182

187-
// Handle the result
188183
console.log(result);
189184
}
190185

@@ -217,7 +212,6 @@ async function run() {
217212
namespace: "<value>",
218213
});
219214

220-
// Handle the result
221215
console.log(result);
222216
}
223217

@@ -380,7 +374,6 @@ async function run() {
380374
},
381375
});
382376

383-
// Handle the result
384377
console.log(result);
385378
}
386379

@@ -411,7 +404,6 @@ async function run() {
411404
namespace: "<value>",
412405
});
413406

414-
// Handle the result
415407
console.log(result);
416408
}
417409

@@ -423,88 +415,45 @@ run();
423415
<!-- Start Error Handling [errors] -->
424416
## Error Handling
425417

426-
Some methods specify known errors which can be thrown. All the known errors are enumerated in the `models/errors/errors.ts` module. The known errors for a method are documented under the *Errors* tables in SDK docs. For example, the `getv1ApisNamespace` method may throw the following errors:
427-
428-
| Error Type | Status Code | Content Type |
429-
| ------------------------------ | ----------- | ---------------- |
430-
| errors.FourHundred | 400 | application/json |
431-
| errors.FourHundredAndOne | 401 | application/json |
432-
| errors.FourHundredAndThree | 403 | application/json |
433-
| errors.FourHundredAndFour | 404 | application/json |
434-
| errors.FourHundredAndTwentyTwo | 422 | application/json |
435-
| errors.FiveHundred | 500 | application/json |
436-
| errors.APIError | 4XX, 5XX | \*/\* |
418+
[`ScalarError`](./src/models/errors/scalarerror.ts) is the base class for all HTTP error responses. It has the following properties:
437419

438-
If the method throws an error and it is not captured by the known errors, it will default to throwing a `APIError`.
420+
| Property | Type | Description |
421+
| ------------------- | ---------- | --------------------------------------------------------------------------------------- |
422+
| `error.message` | `string` | Error message |
423+
| `error.statusCode` | `number` | HTTP response status code eg `404` |
424+
| `error.headers` | `Headers` | HTTP response headers |
425+
| `error.body` | `string` | HTTP body. Can be empty string if no body is returned. |
426+
| `error.rawResponse` | `Response` | Raw HTTP response |
427+
| `error.data$` | | Optional. Some errors may contain structured data. [See Error Classes](#error-classes). |
439428

429+
### Example
440430
```typescript
441431
import { Scalar } from "@scalar/sdk";
442-
import {
443-
FiveHundred,
444-
FourHundred,
445-
FourHundredAndFour,
446-
FourHundredAndOne,
447-
FourHundredAndThree,
448-
FourHundredAndTwentyTwo,
449-
SDKValidationError,
450-
} from "@scalar/sdk/models/errors";
432+
import * as errors from "@scalar/sdk/models/errors";
451433

452434
const scalar = new Scalar({
453435
bearerAuth: "<YOUR_BEARER_TOKEN_HERE>",
454436
});
455437

456438
async function run() {
457-
let result;
458439
try {
459-
result = await scalar.apiDocs.getv1ApisNamespace({
440+
const result = await scalar.apiDocs.getv1ApisNamespace({
460441
namespace: "<value>",
461442
});
462443

463-
// Handle the result
464444
console.log(result);
465-
} catch (err) {
466-
switch (true) {
467-
// The server response does not match the expected SDK schema
468-
case (err instanceof SDKValidationError): {
469-
// Pretty-print will provide a human-readable multi-line error message
470-
console.error(err.pretty());
471-
// Raw value may also be inspected
472-
console.error(err.rawValue);
473-
return;
474-
}
475-
case (err instanceof FourHundred): {
476-
// Handle err.data$: FourHundredData
477-
console.error(err);
478-
return;
479-
}
480-
case (err instanceof FourHundredAndOne): {
481-
// Handle err.data$: FourHundredAndOneData
482-
console.error(err);
483-
return;
484-
}
485-
case (err instanceof FourHundredAndThree): {
486-
// Handle err.data$: FourHundredAndThreeData
487-
console.error(err);
488-
return;
489-
}
490-
case (err instanceof FourHundredAndFour): {
491-
// Handle err.data$: FourHundredAndFourData
492-
console.error(err);
493-
return;
494-
}
495-
case (err instanceof FourHundredAndTwentyTwo): {
496-
// Handle err.data$: FourHundredAndTwentyTwoData
497-
console.error(err);
498-
return;
499-
}
500-
case (err instanceof FiveHundred): {
501-
// Handle err.data$: FiveHundredData
502-
console.error(err);
503-
return;
504-
}
505-
default: {
506-
// Other errors such as network errors, see HTTPClientErrors for more details
507-
throw err;
445+
} catch (error) {
446+
// The base class for HTTP error responses
447+
if (error instanceof errors.ScalarError) {
448+
console.log(error.message);
449+
console.log(error.statusCode);
450+
console.log(error.body);
451+
console.log(error.headers);
452+
453+
// Depending on the method different errors may be thrown
454+
if (error instanceof errors.FourHundred) {
455+
console.log(error.data$.message); // string
456+
console.log(error.data$.code); // string
508457
}
509458
}
510459
}
@@ -514,17 +463,32 @@ run();
514463

515464
```
516465

517-
Validation errors can also occur when either method arguments or data returned from the server do not match the expected format. The `SDKValidationError` that is thrown as a result will capture the raw value that failed validation in an attribute called `rawValue`. Additionally, a `pretty()` method is available on this error that can be used to log a nicely formatted multi-line string since validation errors can list many issues and the plain error string may be difficult read when debugging.
466+
### Error Classes
467+
**Primary errors:**
468+
* [`ScalarError`](./src/models/errors/scalarerror.ts): The base class for HTTP error responses.
469+
* [`FourHundred`](docs/models/errors/fourhundred.md): Bad request. Status code `400`.
470+
* [`FourHundredAndOne`](docs/models/errors/fourhundredandone.md): No auth. Status code `401`.
471+
* [`FourHundredAndThree`](docs/models/errors/fourhundredandthree.md): Forbidden. Status code `403`.
472+
* [`FourHundredAndFour`](docs/models/errors/fourhundredandfour.md): Not found. Status code `404`.
473+
* [`FourHundredAndTwentyTwo`](docs/models/errors/fourhundredandtwentytwo.md): Invalid payload. Status code `422`.
474+
* [`FiveHundred`](docs/models/errors/fivehundred.md): Uncaught error. Status code `500`.
475+
476+
<details><summary>Less common errors (6)</summary>
477+
478+
<br />
479+
480+
**Network errors:**
481+
* [`ConnectionError`](./src/models/errors/httpclienterrors.ts): HTTP client was unable to make a request to a server.
482+
* [`RequestTimeoutError`](./src/models/errors/httpclienterrors.ts): HTTP request timed out due to an AbortSignal signal.
483+
* [`RequestAbortedError`](./src/models/errors/httpclienterrors.ts): HTTP request was aborted by the client.
484+
* [`InvalidRequestError`](./src/models/errors/httpclienterrors.ts): Any input used to create a request is invalid.
485+
* [`UnexpectedClientError`](./src/models/errors/httpclienterrors.ts): Unrecognised or unexpected error.
518486

519-
In some rare cases, the SDK can fail to get a response from the server or even make the request due to unexpected circumstances such as network conditions. These types of errors are captured in the `models/errors/httpclienterrors.ts` module:
520487

521-
| HTTP Client Error | Description |
522-
| ---------------------------------------------------- | ---------------------------------------------------- |
523-
| RequestAbortedError | HTTP request was aborted by the client |
524-
| RequestTimeoutError | HTTP request timed out due to an AbortSignal signal |
525-
| ConnectionError | HTTP client was unable to make a request to a server |
526-
| InvalidRequestError | Any input used to create a request is invalid |
527-
| UnexpectedClientError | Unrecognised or unexpected error |
488+
**Inherit from [`ScalarError`](./src/models/errors/scalarerror.ts)**:
489+
* [`ResponseValidationError`](./src/models/errors/responsevalidationerror.ts): Type mismatch between the data returned from the server and the structure expected by the SDK. See `error.rawValue` for the raw value and `error.pretty()` for a nicely formatted multi-line string.
490+
491+
</details>
528492
<!-- End Error Handling [errors] -->
529493

530494
<!-- Start Server Selection [server] -->
@@ -546,7 +510,6 @@ async function run() {
546510
namespace: "<value>",
547511
});
548512

549-
// Handle the result
550513
console.log(result);
551514
}
552515

USAGE.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ async function run() {
1111
namespace: "<value>",
1212
});
1313

14-
// Handle the result
1514
console.log(result);
1615
}
1716

0 commit comments

Comments
 (0)