From e33d8a7f89e380cd368dc873473ac762b6f10442 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 15 Jan 2025 15:43:35 +0000 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=90=9D=20Update=20SDK=20-=20Gene?= =?UTF-8?q?rate=200.19.0=20(#136)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit > [!IMPORTANT] > Linting report available at: > OpenAPI Change report available at: # SDK update Based on: - OpenAPI Doc - Speakeasy CLI 1.468.6 (2.493.13) https://github.com/speakeasy-api/speakeasy ## Versioning Version Bump Type: [minor] - 🤖 (automated) ## OpenAPI Change Summary ``` ├─┬Info │ └──[🔀] version (5:16) ├─┬Paths │ └─┬/general/v0/general │ └─┬POST │ └─┬Responses │ └─┬200 │ ├──[➕] content (76:27) │ └─┬application/json │ └─┬Schema │ └─┬Extensions │ └──[➕] x-speakeasy-name-override (73:48) └─┬Components ├──[➕] schemas (526:22) └─┬partition_parameters ├──[➕] properties (337:11) ├─┬strategy │ └──[➕] enum (326:15) └─┬unique_element_ids └──[🔀] title (352:22) ``` | Document Element | Total Changes | Breaking Changes | |------------------|---------------|------------------| | components | 4 | 0 | | info | 1 | 0 | | paths | 2 | 0 | ## TYPESCRIPT CHANGELOG ## sdkHooks: 0.2.0 - 2024-11-02 ### :bee: New Features - allow hooks to trigger retries *(commit by [@disintegrator](https://github.com/disintegrator))* ## envVarSecurityUsage: 0.1.2 - 2024-11-04 ### :bug: Bug Fixes - speakeasy examples override default strings *(commit by [@ryan-timothy-albert](https://github.com/ryan-timothy-albert))* ## examples: 2.81.5 - 2025-01-08 ### :bug: Bug Fixes - prevent map examples from retaining obsolete key-value pairs *(commit by [@2ynn](https://github.com/2ynn))* ## acceptHeaders: 2.81.2 - 2024-04-30 ### :bug: Bug Fixes - compute accept types from success responses *(commit by [@disintegrator](https://github.com/disintegrator))* ## globalSecurity: 2.82.12 - 2024-12-23 ### :bug: Bug Fixes - remove default basic auth username / password *(commit by [@disintegrator](https://github.com/disintegrator))* ## core: 3.18.14 - 2025-01-13 ### :bug: Bug Fixes - skip null and undefined parameter values during serialization *(commit by [@disintegrator](https://github.com/disintegrator))* ## core: 3.18.13 - 2025-01-13 ### :bug: Bug Fixes - avoid setting `Content-Type: */*` request header for relevant operations *(commit by [@disintegrator](https://github.com/disintegrator))* ## core: 3.18.12 - 2024-12-19 ### :wrench: Chores - bump typescript transpilation target to ES2020 *(commit by [@mfbx9da4](https://github.com/mfbx9da4))* ## core: 3.18.11 - 2024-12-16 ### :bug: Bug Fixes - flattening with hidden parameters *(commit by [@mfbx9da4](https://github.com/mfbx9da4))* ## core: 3.18.10 - 2024-12-13 ### :bug: Bug Fixes - Support OAS references under multipart/form-data file properties *(commit by [@bflad](https://github.com/bflad))* ## core: 3.18.9 - 2024-12-06 ### :bug: Bug Fixes - support property names solely comprised of illegal characters *(commit by [@mfbx9da4](https://github.com/mfbx9da4))* ## core: 3.18.8 - 2024-11-28 ### :bug: Bug Fixes - add arguments to variable name exclusion list to avoid shadowing errors *(commit by [@tristanspeakeasy](https://github.com/tristanspeakeasy))* ## core: 3.18.7 - 2024-11-22 ### :bug: Bug Fixes - handle circular references through unions correctly *(commit by [@disintegrator](https://github.com/disintegrator))* ## core: 3.18.6 - 2024-11-19 ### :bug: Bug Fixes - detect import cycles between models living in different modules *(commit by [@disintegrator](https://github.com/disintegrator))* ## core: 3.18.5 - 2024-11-15 ### :bug: Bug Fixes - handling of raw buffers when deserialzing into zod types *(commit by [@tristanspeakeasy](https://github.com/tristanspeakeasy))* ## core: 3.18.4 - 2024-11-12 ### :bug: Bug Fixes - sort media type objects by specificity *(commit by [@disintegrator](https://github.com/disintegrator))* ## core: 3.18.3 - 2024-10-31 ### :bug: Bug Fixes - Prevent panic with invalid implicit discriminator mappings with propertyName within allOf object *(commit by [@bflad](https://github.com/bflad))* ## unions: 2.85.8 - 2024-11-05 ### :bug: Bug Fixes - improved handling of complex allOf schemas that merge multiple types *(commit by [@tristanspeakeasy](https://github.com/tristanspeakeasy))* Co-authored-by: speakeasybot --- .gitignore | 2 + .npmignore | 3 + .speakeasy/gen.lock | 31 +-- .speakeasy/workflow.lock | 17 +- .speakeasy/workflow.yaml | 2 + README.md | 22 +- RELEASES.md | 12 +- RUNTIMES.md | 28 ++- docs/lib/utils/retryconfig.md | 24 ++ .../models/operations/partitionresponse.md | 13 +- docs/sdk/models/shared/partitionparameters.md | 1 + docs/sdk/models/shared/strategy.md | 1 + docs/sdks/general/README.md | 102 +++++++++ docs/sdks/unstructuredclient/README.md | 5 + gen.yaml | 6 +- jsr.json | 2 +- package-lock.json | 4 +- package.json | 2 +- src/funcs/generalPartition.ts | 212 +++++++++++------- src/hooks/hooks.ts | 20 ++ src/hooks/types.ts | 11 + src/lib/config.ts | 8 +- src/lib/encodings.ts | 156 ++++++++----- src/lib/matchers.ts | 2 +- src/lib/primitives.ts | 96 ++++++++ src/lib/retries.ts | 94 +++++--- src/lib/sdks.ts | 46 ++-- src/lib/security.ts | 44 +++- src/sdk/general.ts | 9 +- src/sdk/models/errors/httpvalidationerror.ts | 17 ++ src/sdk/models/operations/partition.ts | 64 +++++- src/sdk/models/shared/partitionparameters.ts | 45 ++++ src/sdk/models/shared/security.ts | 17 ++ src/sdk/models/shared/validationerror.ts | 33 +++ src/sdk/types/operations.ts | 16 +- tsconfig.json | 3 +- 36 files changed, 907 insertions(+), 263 deletions(-) create mode 100644 docs/lib/utils/retryconfig.md create mode 100644 docs/sdks/general/README.md create mode 100644 docs/sdks/unstructuredclient/README.md diff --git a/.gitignore b/.gitignore index 0d4c8e9..5a69c87 100755 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +/react-query +/.speakeasy/reports /__tests__ /funcs /core.* diff --git a/.npmignore b/.npmignore index 06cd8ab..9ad259e 100644 --- a/.npmignore +++ b/.npmignore @@ -1,4 +1,7 @@ **/* +!/FUNCTIONS.md +!/RUNTIMES.md +!/REACT_QUERY.md !/**/*.ts !/**/*.js !/**/*.map diff --git a/.speakeasy/gen.lock b/.speakeasy/gen.lock index 46cb1a0..595f749 100755 --- a/.speakeasy/gen.lock +++ b/.speakeasy/gen.lock @@ -1,26 +1,27 @@ lockVersion: 2.0.0 id: f42cb8e6-e2ce-4565-b975-5a9f38b94d5a management: - docChecksum: 0ecb1bbbef6c9805d6a3edcc0afd098a - docVersion: 1.0.51 - speakeasyVersion: 1.422.1 - generationVersion: 2.438.15 - releaseVersion: 0.18.2 - configChecksum: ca25abf7161ef39bf7793f0e79caf68e + docChecksum: 4717c00d28425bfad21b5c8706ed4ca7 + docVersion: 1.0.58 + speakeasyVersion: 1.468.6 + generationVersion: 2.493.13 + releaseVersion: 0.19.0 + configChecksum: 756522998ca4a24ef07ae29599125e94 repoURL: https://github.com/Unstructured-IO/unstructured-js-client.git repoSubDirectory: . installationURL: https://github.com/Unstructured-IO/unstructured-js-client published: true features: typescript: + acceptHeaders: 2.81.2 additionalDependencies: 0.1.0 constsAndDefaults: 0.1.11 - core: 3.18.2 + core: 3.18.14 defaultEnabledRetries: 0.1.0 enumUnions: 0.1.0 - envVarSecurityUsage: 0.1.1 - examples: 2.81.4 - globalSecurity: 2.82.11 + envVarSecurityUsage: 0.1.2 + examples: 2.81.5 + globalSecurity: 2.82.12 globalSecurityCallbacks: 0.1.0 globalServerURLs: 2.82.4 nameOverrides: 2.81.2 @@ -28,18 +29,18 @@ features: openEnums: 0.1.1 responseFormat: 0.2.3 retries: 2.83.0 - sdkHooks: 0.1.0 + sdkHooks: 0.2.0 serverIDs: 2.81.2 - unions: 2.85.7 + unions: 2.85.8 uploadStreams: 0.1.0 generatedFiles: - .eslintrc.cjs - .gitattributes - .npmignore - - CONTRIBUTING.md - FUNCTIONS.md - RUNTIMES.md - USAGE.md + - docs/lib/utils/retryconfig.md - docs/sdk/models/errors/detail.md - docs/sdk/models/errors/httpvalidationerror.md - docs/sdk/models/errors/servererror.md @@ -53,6 +54,8 @@ generatedFiles: - docs/sdk/models/shared/security.md - docs/sdk/models/shared/strategy.md - docs/sdk/models/shared/validationerror.md + - docs/sdks/general/README.md + - docs/sdks/unstructuredclient/README.md - jsr.json - package.json - src/core.ts @@ -108,7 +111,9 @@ examples: responses: "200": application/json: [{"type": "Title", "element_id": "6aa0ff22f91bbe7e26e8e25ca8052acd", "text": "LayoutParser: A Unified Toolkit for Deep Learning Based Document Image Analysis", "metadata": {"languages": ["eng"], "page_number": 1, "filename": "layout-parser-paper.pdf", "filetype": "application/pdf"}}] + text/csv: "" "422": application/json: {"detail": []} 5XX: application/json: {"detail": "An error occurred"} +generatedTests: {} diff --git a/.speakeasy/workflow.lock b/.speakeasy/workflow.lock index 599276d..995f0c5 100644 --- a/.speakeasy/workflow.lock +++ b/.speakeasy/workflow.lock @@ -1,18 +1,21 @@ -speakeasyVersion: 1.422.1 +speakeasyVersion: 1.468.6 sources: my-source: sourceNamespace: my-source - sourceRevisionDigest: sha256:7904e30126e3e30e77b56f6d901513e7009cbdc1d433dd7f6baa7da71a31b79a - sourceBlobDigest: sha256:4747157b44baaa540a909f6947d16b08b096845a816819d9b0d35d074561fb68 + sourceRevisionDigest: sha256:3ce862c538447f14371df47b3eb07ae3ac20d1dd7f127ecaafbdbda175e5f967 + sourceBlobDigest: sha256:5b768ce3d603f07d017095a42738571836d3d0698c1713bf9b184a6235cffdd2 tags: - latest - - main + - speakeasy-sdk-regen-1731198560 + - 1.0.58 targets: unstructed-typescript: source: my-source sourceNamespace: my-source - sourceRevisionDigest: sha256:7904e30126e3e30e77b56f6d901513e7009cbdc1d433dd7f6baa7da71a31b79a - sourceBlobDigest: sha256:4747157b44baaa540a909f6947d16b08b096845a816819d9b0d35d074561fb68 + sourceRevisionDigest: sha256:3ce862c538447f14371df47b3eb07ae3ac20d1dd7f127ecaafbdbda175e5f967 + sourceBlobDigest: sha256:5b768ce3d603f07d017095a42738571836d3d0698c1713bf9b184a6235cffdd2 + codeSamplesNamespace: my-source-typescript-code-samples + codeSamplesRevisionDigest: sha256:36362ca76ec0ffe7ab5d84c904a5e4ecb16158194fedd282fa1c2bb0399b91fa workflow: workflowVersion: 1.0.0 speakeasyVersion: latest @@ -33,3 +36,5 @@ workflow: token: $NPM_TOKEN codeSamples: output: codeSamples.yaml + registry: + location: registry.speakeasyapi.dev/unstructured/unstructured5xr/my-source-typescript-code-samples diff --git a/.speakeasy/workflow.yaml b/.speakeasy/workflow.yaml index fe4afae..7d965c7 100644 --- a/.speakeasy/workflow.yaml +++ b/.speakeasy/workflow.yaml @@ -17,3 +17,5 @@ targets: token: $NPM_TOKEN codeSamples: output: codeSamples.yaml + registry: + location: registry.speakeasyapi.dev/unstructured/unstructured5xr/my-source-typescript-code-samples diff --git a/README.md b/README.md index 0b99c8b..2781d31 100755 --- a/README.md +++ b/README.md @@ -268,19 +268,17 @@ client.general.partition({ ## Table of Contents + + * [SDK Installation](#sdk-installation) + * [SDK Example Usage](#sdk-example-usage) + * [Change the base URL](#change-the-base-url) + * [Custom HTTP Client](#custom-http-client) + * [Retries](#retries) + * [Requirements](#requirements) + * [Standalone functions](#standalone-functions) + * [File uploads](#file-uploads) + * [Debugging](#debugging) -* [SDK Installation](#sdk-installation) -* [Requirements](#requirements) -* [SDK Example Usage](#sdk-example-usage) -* [Available Resources and Operations](#available-resources-and-operations) -* [Standalone functions](#standalone-functions) -* [File uploads](#file-uploads) -* [Retries](#retries) -* [Error Handling](#error-handling) -* [Server Selection](#server-selection) -* [Custom HTTP Client](#custom-http-client) -* [Authentication](#authentication) -* [Debugging](#debugging) diff --git a/RELEASES.md b/RELEASES.md index 96ee920..0ac0a7f 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -504,4 +504,14 @@ Based on: ### Generated - [typescript v0.18.2] . ### Releases -- [NPM v0.18.2] https://www.npmjs.com/package/unstructured-client/v/0.18.2 - . \ No newline at end of file +- [NPM v0.18.2] https://www.npmjs.com/package/unstructured-client/v/0.18.2 - . + +## 2025-01-15 00:27:25 +### Changes +Based on: +- OpenAPI Doc +- Speakeasy CLI 1.468.6 (2.493.13) https://github.com/speakeasy-api/speakeasy +### Generated +- [typescript v0.19.0] . +### Releases +- [NPM v0.19.0] https://www.npmjs.com/package/unstructured-client/v/0.19.0 - . \ No newline at end of file diff --git a/RUNTIMES.md b/RUNTIMES.md index d08a0c0..db7ea94 100644 --- a/RUNTIMES.md +++ b/RUNTIMES.md @@ -1,6 +1,6 @@ # Supported JavaScript runtimes -This SDK is intended to be used in JavaScript runtimes that support the following features: +This SDK is intended to be used in JavaScript runtimes that support ECMAScript 2020 or newer. The SDK uses the following features: * [Web Fetch API][web-fetch] * [Web Streams API][web-streams] and in particular `ReadableStream` @@ -20,3 +20,29 @@ Runtime environments that are explicitly supported are: - Note that Deno does not currently have native support for streaming file uploads backed by the filesystem ([issue link][deno-file-streaming]) [deno-file-streaming]: https://github.com/denoland/deno/issues/11018 + +## Recommended TypeScript compiler options + +The following `tsconfig.json` options are recommended for projects using this +SDK in order to get static type support for features like async iterables, +streams and `fetch`-related APIs ([`for await...of`][for-await-of], +[`AbortSignal`][abort-signal], [`Request`][request], [`Response`][response] and +so on): + +[for-await-of]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of +[abort-signal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal +[request]: https://developer.mozilla.org/en-US/docs/Web/API/Request +[response]: https://developer.mozilla.org/en-US/docs/Web/API/Response + +```jsonc +{ + "compilerOptions": { + "target": "es2020", // or higher + "lib": ["es2020", "dom", "dom.iterable"], + } +} +``` + +While `target` can be set to older ECMAScript versions, it may result in extra, +unnecessary compatibility code being generated if you are not targeting old +runtimes. \ No newline at end of file diff --git a/docs/lib/utils/retryconfig.md b/docs/lib/utils/retryconfig.md new file mode 100644 index 0000000..08f95f4 --- /dev/null +++ b/docs/lib/utils/retryconfig.md @@ -0,0 +1,24 @@ +# RetryConfig + +Allows customizing the default retry configuration. It is only permitted in methods that accept retry policies. + +## Fields + +| Name | Type | Description | Example | +| ------------------------- | ----------------------------------- | ------------------------------------------------------------------------------------------ | ----------- | +| `strategy` | `"backoff" | "none"` | The retry strategy to use. | `"backoff"` | +| `backoff` | [BackoffStrategy](#backoffstrategy) | When strategy is "backoff", this configurates for the backoff parameters. | | +| `retryConnectionErrors` | `*boolean*` | When strategy is "backoff", this determines whether or not to retry on connection errors. | `true` | + +## BackoffStrategy + +The backoff strategy allows retrying a request with an exponential backoff between each retry. + +### Fields + +| Name | Type | Description | Example | +| ------------------ | ------------ | ----------------------------------------- | -------- | +| `initialInterval` | `*number*` | The initial interval in milliseconds. | `500` | +| `maxInterval` | `*number*` | The maximum interval in milliseconds. | `60000` | +| `exponent` | `*number*` | The exponent to use for the backoff. | `1.5` | +| `maxElapsedTime` | `*number*` | The maximum elapsed time in milliseconds. | `300000` | \ No newline at end of file diff --git a/docs/sdk/models/operations/partitionresponse.md b/docs/sdk/models/operations/partitionresponse.md index dc2a0cc..6b88a25 100644 --- a/docs/sdk/models/operations/partitionresponse.md +++ b/docs/sdk/models/operations/partitionresponse.md @@ -7,6 +7,10 @@ import { PartitionResponse } from "unstructured-client/sdk/models/operations"; let value: PartitionResponse = { contentType: "", + statusCode: 306, + rawResponse: new Response("{\"message\": \"hello world\"}", { + headers: { "Content-Type": "application/json" }, + }), elements: [ { "type": "Title", @@ -23,10 +27,6 @@ let value: PartitionResponse = { }, }, ], - statusCode: 306, - rawResponse: new Response("{\"message\": \"hello world\"}", { - headers: { "Content-Type": "application/json" }, - }), }; ``` @@ -35,6 +35,7 @@ let value: PartitionResponse = { | Field | Type | Required | Description | | --------------------------------------------------------------------- | --------------------------------------------------------------------- | --------------------------------------------------------------------- | --------------------------------------------------------------------- | | `contentType` | *string* | :heavy_check_mark: | HTTP response content type for this operation | -| `elements` | Record[] | :heavy_minus_sign: | Successful Response | | `statusCode` | *number* | :heavy_check_mark: | HTTP response status code for this operation | -| `rawResponse` | [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) | :heavy_check_mark: | Raw HTTP response; suitable for custom response parsing | \ No newline at end of file +| `rawResponse` | [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) | :heavy_check_mark: | Raw HTTP response; suitable for custom response parsing | +| `csvElements` | *string* | :heavy_minus_sign: | Successful Response | +| `elements` | Record[] | :heavy_minus_sign: | Successful Response | \ No newline at end of file diff --git a/docs/sdk/models/shared/partitionparameters.md b/docs/sdk/models/shared/partitionparameters.md index a6cd2c8..5a836ee 100644 --- a/docs/sdk/models/shared/partitionparameters.md +++ b/docs/sdk/models/shared/partitionparameters.md @@ -41,5 +41,6 @@ import { PartitionParameters } from "unstructured-client/sdk/models/shared"; | `splitPdfPageRange` | *number*[] | :heavy_minus_sign: | When `split_pdf_page is set to `True`, this parameter selects a subset of the pdf to send to the API. The parameter is a list of 2 integers within the range [1, length_of_pdf]. An Error is thrown if the given range is invalid. Ignored on backend. | [
1,
10
] | | `startingPageNumber` | *number* | :heavy_minus_sign: | When PDF is split into pages before sending it into the API, providing this information will allow the page number to be assigned correctly. Introduced in 1.0.27. | | | `strategy` | [shared.Strategy](../../../sdk/models/shared/strategy.md) | :heavy_minus_sign: | The strategy to use for partitioning PDF/image. Options are fast, hi_res, auto. Default: hi_res | auto | +| `tableOcrAgent` | *string* | :heavy_minus_sign: | The OCR agent to use for table ocr inference. | | | `uniqueElementIds` | *boolean* | :heavy_minus_sign: | When `True`, assign UUIDs to element IDs, which guarantees their uniqueness (useful when using them as primary keys in database). Otherwise a SHA-256 of element text is used. Default: `False` | | | `xmlKeepTags` | *boolean* | :heavy_minus_sign: | If `True`, will retain the XML tags in the output. Otherwise it will simply extract the text from within the tags. Only applies to XML documents. | | \ No newline at end of file diff --git a/docs/sdk/models/shared/strategy.md b/docs/sdk/models/shared/strategy.md index 43b1ff6..cf1a992 100644 --- a/docs/sdk/models/shared/strategy.md +++ b/docs/sdk/models/shared/strategy.md @@ -20,4 +20,5 @@ This is an open enum. Unrecognized values will be captured as the `Unrecognized< | `HiRes` | hi_res | | `Auto` | auto | | `OcrOnly` | ocr_only | +| `OdOnly` | od_only | | - | `Unrecognized` | \ No newline at end of file diff --git a/docs/sdks/general/README.md b/docs/sdks/general/README.md new file mode 100644 index 0000000..0332c3f --- /dev/null +++ b/docs/sdks/general/README.md @@ -0,0 +1,102 @@ +# General +(*general*) + +## Overview + +### Available Operations + +* [partition](#partition) - Summary + +## partition + +Description + +### Example Usage + +```typescript +import { openAsBlob } from "node:fs"; +import { UnstructuredClient } from "unstructured-client"; +import { ChunkingStrategy, Strategy } from "unstructured-client/sdk/models/shared"; + +const unstructuredClient = new UnstructuredClient(); + +async function run() { + const result = await unstructuredClient.general.partition({ + partitionParameters: { + files: await openAsBlob("example.file"), + chunkingStrategy: ChunkingStrategy.ByTitle, + splitPdfPageRange: [ + 1, + 10, + ], + strategy: Strategy.HiRes, + }, + }); + + // Handle the result + console.log(result); +} + +run(); +``` + +### Standalone function + +The standalone function version of this method: + +```typescript +import { openAsBlob } from "node:fs"; +import { UnstructuredClientCore } from "unstructured-client/core.js"; +import { generalPartition } from "unstructured-client/funcs/generalPartition.js"; +import { ChunkingStrategy, Strategy } from "unstructured-client/sdk/models/shared"; + +// Use `UnstructuredClientCore` for best tree-shaking performance. +// You can create one instance of it to use across an application. +const unstructuredClient = new UnstructuredClientCore(); + +async function run() { + const res = await generalPartition(unstructuredClient, { + partitionParameters: { + files: await openAsBlob("example.file"), + chunkingStrategy: ChunkingStrategy.ByTitle, + splitPdfPageRange: [ + 1, + 10, + ], + strategy: Strategy.HiRes, + }, + }); + + if (!res.ok) { + throw res.error; + } + + const { value: result } = res; + + // Handle the result + console.log(result); +} + +run(); +``` + +### Parameters + +| Parameter | Type | Required | Description | +| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `request` | [operations.PartitionRequest](../../sdk/models/operations/partitionrequest.md) | :heavy_check_mark: | The request object to use for the request. | +| `options` | RequestOptions | :heavy_minus_sign: | Used to set various options for making HTTP requests. | +| `options.fetchOptions` | [RequestInit](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request#options) | :heavy_minus_sign: | Options that are passed to the underlying HTTP request. This can be used to inject extra headers for examples. All `Request` options, except `method` and `body`, are allowed. | +| `options.retries` | [RetryConfig](../../lib/utils/retryconfig.md) | :heavy_minus_sign: | Enables retrying HTTP requests under certain failure conditions. | + +### Response + +**Promise\<[operations.PartitionResponse](../../sdk/models/operations/partitionresponse.md)\>** + +### Errors + +| Error Type | Status Code | Content Type | +| -------------------------- | -------------------------- | -------------------------- | +| errors.HTTPValidationError | 422 | application/json | +| errors.ServerError | 5XX | application/json | +| errors.SDKError | 4XX | \*/\* | \ No newline at end of file diff --git a/docs/sdks/unstructuredclient/README.md b/docs/sdks/unstructuredclient/README.md new file mode 100644 index 0000000..895459f --- /dev/null +++ b/docs/sdks/unstructuredclient/README.md @@ -0,0 +1,5 @@ +# UnstructuredClient SDK + +## Overview + +### Available Operations diff --git a/gen.yaml b/gen.yaml index 81c95b8..2ba7661 100644 --- a/gen.yaml +++ b/gen.yaml @@ -9,8 +9,9 @@ generation: requestResponseComponentNamesFeb2024: false auth: oAuth2ClientCredentialsEnabled: false + oAuth2PasswordEnabled: false typescript: - version: 0.18.2 + version: 0.19.0 additionalDependencies: dependencies: async: ^3.2.5 @@ -22,6 +23,9 @@ typescript: additionalPackageJSON: {} author: Unstructured clientServerStatusCodesAsErrors: true + defaultErrorName: SDKError + enableCustomCodeRegions: false + enableReactQuery: false enumFormat: enum flattenGlobalSecurity: false flatteningOrder: body-first diff --git a/jsr.json b/jsr.json index d7e408d..bfa1ace 100644 --- a/jsr.json +++ b/jsr.json @@ -2,7 +2,7 @@ { "name": "unstructured-client", - "version": "0.18.2", + "version": "0.19.0", "exports": { ".": "./src/index.ts", "./sdk/models/errors": "./src/sdk/models/errors/index.ts", diff --git a/package-lock.json b/package-lock.json index 05cf794..cc79569 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "unstructured-client", - "version": "0.18.2", + "version": "0.19.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "unstructured-client", - "version": "0.18.2", + "version": "0.19.0", "dependencies": { "async": "^3.2.5", "pdf-lib": "^1.17.1" diff --git a/package.json b/package.json index 4c243d2..f5695c2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "unstructured-client", - "version": "0.18.2", + "version": "0.19.0", "author": "Unstructured", "main": "./index.js", "sideEffects": false, diff --git a/src/funcs/generalPartition.ts b/src/funcs/generalPartition.ts index fb40638..5be4074 100644 --- a/src/funcs/generalPartition.ts +++ b/src/funcs/generalPartition.ts @@ -3,9 +3,10 @@ */ import { UnstructuredClientCore } from "../core.js"; -import { encodeSimple } from "../lib/encodings.js"; +import { appendForm, encodeSimple } from "../lib/encodings.js"; import { readableStreamToArrayBuffer } from "../lib/files.js"; import * as M from "../lib/matchers.js"; +import { compactMap } from "../lib/primitives.js"; import { safeParse } from "../lib/schemas.js"; import { RequestOptions } from "../lib/sdks.js"; import { extractSecurity, resolveGlobalSecurity } from "../lib/security.js"; @@ -25,6 +26,11 @@ import { isBlobLike } from "../sdk/types/blobs.js"; import { Result } from "../sdk/types/fp.js"; import { isReadableStream } from "../sdk/types/streams.js"; +export enum PartitionAcceptEnum { + applicationJson = "application/json", + textCsv = "text/csv", +} + /** * Summary * @@ -34,7 +40,7 @@ import { isReadableStream } from "../sdk/types/streams.js"; export async function generalPartition( client: UnstructuredClientCore, request: operations.PartitionRequest, - options?: RequestOptions, + options?: RequestOptions & { acceptHeaderOverride?: PartitionAcceptEnum }, ): Promise< Result< operations.PartitionResponse, @@ -61,15 +67,16 @@ export async function generalPartition( const body = new FormData(); if (isBlobLike(payload.partition_parameters.files)) { - body.append("files", payload.partition_parameters.files); + appendForm(body, "files", payload.partition_parameters.files); } else if (isReadableStream(payload.partition_parameters.files.content)) { const buffer = await readableStreamToArrayBuffer( payload.partition_parameters.files.content, ); const blob = new Blob([buffer], { type: "application/octet-stream" }); - body.append("files", blob); + appendForm(body, "files", blob); } else { - body.append( + appendForm( + body, "files", new Blob([payload.partition_parameters.files.content], { type: "application/octet-stream", @@ -78,193 +85,237 @@ export async function generalPartition( ); } if (payload.partition_parameters.chunking_strategy !== undefined) { - body.append( + appendForm( + body, "chunking_strategy", - String(payload.partition_parameters.chunking_strategy), + payload.partition_parameters.chunking_strategy, ); } if (payload.partition_parameters.combine_under_n_chars !== undefined) { - body.append( + appendForm( + body, "combine_under_n_chars", - String(payload.partition_parameters.combine_under_n_chars), + payload.partition_parameters.combine_under_n_chars, ); } if (payload.partition_parameters.content_type !== undefined) { - body.append( - "content_type", - String(payload.partition_parameters.content_type), - ); + appendForm(body, "content_type", payload.partition_parameters.content_type); } if (payload.partition_parameters.coordinates !== undefined) { - body.append( - "coordinates", - String(payload.partition_parameters.coordinates), - ); + appendForm(body, "coordinates", payload.partition_parameters.coordinates); } if (payload.partition_parameters.encoding !== undefined) { - body.append("encoding", String(payload.partition_parameters.encoding)); + appendForm(body, "encoding", payload.partition_parameters.encoding); } if (payload.partition_parameters.extract_image_block_types !== undefined) { - body.append( + appendForm( + body, "extract_image_block_types", - String(payload.partition_parameters.extract_image_block_types), + payload.partition_parameters.extract_image_block_types, ); } if (payload.partition_parameters.gz_uncompressed_content_type !== undefined) { - body.append( + appendForm( + body, "gz_uncompressed_content_type", - String(payload.partition_parameters.gz_uncompressed_content_type), + payload.partition_parameters.gz_uncompressed_content_type, ); } if (payload.partition_parameters.hi_res_model_name !== undefined) { - body.append( + appendForm( + body, "hi_res_model_name", - String(payload.partition_parameters.hi_res_model_name), + payload.partition_parameters.hi_res_model_name, ); } if (payload.partition_parameters.include_orig_elements !== undefined) { - body.append( + appendForm( + body, "include_orig_elements", - String(payload.partition_parameters.include_orig_elements), + payload.partition_parameters.include_orig_elements, ); } if (payload.partition_parameters.include_page_breaks !== undefined) { - body.append( + appendForm( + body, "include_page_breaks", - String(payload.partition_parameters.include_page_breaks), + payload.partition_parameters.include_page_breaks, ); } if (payload.partition_parameters.include_slide_notes !== undefined) { - body.append( + appendForm( + body, "include_slide_notes", - String(payload.partition_parameters.include_slide_notes), + payload.partition_parameters.include_slide_notes, ); } if (payload.partition_parameters.languages !== undefined) { - body.append("languages", String(payload.partition_parameters.languages)); + appendForm(body, "languages", payload.partition_parameters.languages); } if (payload.partition_parameters.max_characters !== undefined) { - body.append( + appendForm( + body, "max_characters", - String(payload.partition_parameters.max_characters), + payload.partition_parameters.max_characters, ); } if (payload.partition_parameters.multipage_sections !== undefined) { - body.append( + appendForm( + body, "multipage_sections", - String(payload.partition_parameters.multipage_sections), + payload.partition_parameters.multipage_sections, ); } if (payload.partition_parameters.new_after_n_chars !== undefined) { - body.append( + appendForm( + body, "new_after_n_chars", - String(payload.partition_parameters.new_after_n_chars), + payload.partition_parameters.new_after_n_chars, ); } if (payload.partition_parameters.ocr_languages !== undefined) { - body.append( + appendForm( + body, "ocr_languages", - String(payload.partition_parameters.ocr_languages), + payload.partition_parameters.ocr_languages, ); } if (payload.partition_parameters.output_format !== undefined) { - body.append("output_format", payload.partition_parameters.output_format); + appendForm( + body, + "output_format", + payload.partition_parameters.output_format, + ); } if (payload.partition_parameters.overlap !== undefined) { - body.append("overlap", String(payload.partition_parameters.overlap)); + appendForm(body, "overlap", payload.partition_parameters.overlap); } if (payload.partition_parameters.overlap_all !== undefined) { - body.append( - "overlap_all", - String(payload.partition_parameters.overlap_all), - ); + appendForm(body, "overlap_all", payload.partition_parameters.overlap_all); } if (payload.partition_parameters.pdf_infer_table_structure !== undefined) { - body.append( + appendForm( + body, "pdf_infer_table_structure", - String(payload.partition_parameters.pdf_infer_table_structure), + payload.partition_parameters.pdf_infer_table_structure, ); } if (payload.partition_parameters.similarity_threshold !== undefined) { - body.append( + appendForm( + body, "similarity_threshold", - String(payload.partition_parameters.similarity_threshold), + payload.partition_parameters.similarity_threshold, ); } if (payload.partition_parameters.skip_infer_table_types !== undefined) { - body.append( + appendForm( + body, "skip_infer_table_types", - String(payload.partition_parameters.skip_infer_table_types), + payload.partition_parameters.skip_infer_table_types, ); } if (payload.partition_parameters.split_pdf_allow_failed !== undefined) { - body.append( + appendForm( + body, "split_pdf_allow_failed", - String(payload.partition_parameters.split_pdf_allow_failed), + payload.partition_parameters.split_pdf_allow_failed, ); } if (payload.partition_parameters.split_pdf_concurrency_level !== undefined) { - body.append( + appendForm( + body, "split_pdf_concurrency_level", - String(payload.partition_parameters.split_pdf_concurrency_level), + payload.partition_parameters.split_pdf_concurrency_level, ); } if (payload.partition_parameters.split_pdf_page !== undefined) { - body.append( + appendForm( + body, "split_pdf_page", - String(payload.partition_parameters.split_pdf_page), + payload.partition_parameters.split_pdf_page, ); } if (payload.partition_parameters.split_pdf_page_range !== undefined) { - body.append( + appendForm( + body, "split_pdf_page_range", - String(payload.partition_parameters.split_pdf_page_range), + payload.partition_parameters.split_pdf_page_range, ); } if (payload.partition_parameters.starting_page_number !== undefined) { - body.append( + appendForm( + body, "starting_page_number", - String(payload.partition_parameters.starting_page_number), + payload.partition_parameters.starting_page_number, ); } if (payload.partition_parameters.strategy !== undefined) { - body.append("strategy", payload.partition_parameters.strategy); + appendForm(body, "strategy", payload.partition_parameters.strategy); + } + if (payload.partition_parameters.table_ocr_agent !== undefined) { + appendForm( + body, + "table_ocr_agent", + payload.partition_parameters.table_ocr_agent, + ); } if (payload.partition_parameters.unique_element_ids !== undefined) { - body.append( + appendForm( + body, "unique_element_ids", - String(payload.partition_parameters.unique_element_ids), + payload.partition_parameters.unique_element_ids, ); } if (payload.partition_parameters.xml_keep_tags !== undefined) { - body.append( + appendForm( + body, "xml_keep_tags", - String(payload.partition_parameters.xml_keep_tags), + payload.partition_parameters.xml_keep_tags, ); } const path = pathToFunc("/general/v0/general")(); - const headers = new Headers({ - Accept: "application/json", + const headers = new Headers(compactMap({ + Accept: options?.acceptHeaderOverride + || "application/json;q=1, text/csv;q=0", "unstructured-api-key": encodeSimple( "unstructured-api-key", payload["unstructured-api-key"], { explode: false, charEncoding: "none" }, ), - }); + })); const securityInput = await extractSecurity(client._options.security); + const requestSecurity = resolveGlobalSecurity(securityInput); + const context = { operationID: "partition", oAuth2Scopes: [], + + resolvedSecurity: requestSecurity, + securitySource: client._options.security, + retryConfig: options?.retries + || client._options.retryConfig + || { + strategy: "backoff", + backoff: { + initialInterval: 3000, + maxInterval: 720000, + exponent: 1.88, + maxElapsedTime: 1800000, + }, + retryConnectionErrors: true, + } + || { strategy: "none" }, + retryCodes: options?.retryCodes || ["502", "503", "504"], }; - const requestSecurity = resolveGlobalSecurity(securityInput); const requestRes = client._createRequest(context, { security: requestSecurity, method: "POST", + baseURL: options?.serverURL, path: path, headers: headers, body: body, @@ -278,19 +329,8 @@ export async function generalPartition( const doResult = await client._do(req, { context, errorCodes: ["422", "4XX", "5XX"], - retryConfig: options?.retries - || client._options.retryConfig - || { - strategy: "backoff", - backoff: { - initialInterval: 3000, - maxInterval: 720000, - exponent: 1.88, - maxElapsedTime: 1800000, - }, - retryConnectionErrors: true, - }, - retryCodes: options?.retryCodes || ["502", "503", "504"], + retryConfig: context.retryConfig, + retryCodes: context.retryCodes, }); if (!doResult.ok) { return doResult; @@ -318,7 +358,11 @@ export async function generalPartition( | ConnectionError >( M.json(200, operations.PartitionResponse$inboundSchema, { - key: "Elements", + key: "elements", + }), + M.text(200, operations.PartitionResponse$inboundSchema, { + ctype: "text/csv", + key: "csv_elements", }), M.jsonErr(422, errors.HTTPValidationError$inboundSchema), M.fail("4XX"), diff --git a/src/hooks/hooks.ts b/src/hooks/hooks.ts index d1b3396..d34c884 100644 --- a/src/hooks/hooks.ts +++ b/src/hooks/hooks.ts @@ -12,6 +12,7 @@ import { BeforeCreateRequestHook, BeforeRequestContext, BeforeRequestHook, + Hook, Hooks, SDKInitHook, SDKInitOptions, @@ -27,6 +28,25 @@ export class SDKHooks implements Hooks { afterErrorHooks: AfterErrorHook[] = []; constructor() { + const presetHooks: Array = []; + + for (const hook of presetHooks) { + if ("sdkInit" in hook) { + this.registerSDKInitHook(hook); + } + if ("beforeCreateRequest" in hook) { + this.registerBeforeCreateRequestHook(hook); + } + if ("beforeRequest" in hook) { + this.registerBeforeRequestHook(hook); + } + if ("afterSuccess" in hook) { + this.registerAfterSuccessHook(hook); + } + if ("afterError" in hook) { + this.registerAfterErrorHook(hook); + } + } initHooks(this); } diff --git a/src/hooks/types.ts b/src/hooks/types.ts index 656b9e3..8a27c65 100644 --- a/src/hooks/types.ts +++ b/src/hooks/types.ts @@ -3,11 +3,15 @@ */ import { HTTPClient, RequestInput } from "../lib/http.js"; +import { RetryConfig } from "../lib/retries.js"; +import { SecurityState } from "../lib/security.js"; export type HookContext = { operationID: string; oAuth2Scopes?: string[]; securitySource?: any | (() => Promise); + retryConfig: RetryConfig; + resolvedSecurity: SecurityState | null; }; export type Awaitable = T | Promise; @@ -96,3 +100,10 @@ export interface Hooks { /** Registers a hook to be used by the SDK for the after error event. */ registerAfterErrorHook(hook: AfterErrorHook): void; } + +export type Hook = + | SDKInitHook + | BeforeCreateRequestHook + | BeforeRequestHook + | AfterSuccessHook + | AfterErrorHook; diff --git a/src/lib/config.ts b/src/lib/config.ts index 0b5354a..e9bba90 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -68,9 +68,9 @@ export function serverURLFromOptions(options: SDKOptions): URL | null { export const SDK_METADATA = { language: "typescript", - openapiDocVersion: "1.0.51", - sdkVersion: "0.18.2", - genVersion: "2.438.15", + openapiDocVersion: "1.0.58", + sdkVersion: "0.19.0", + genVersion: "2.493.13", userAgent: - "speakeasy-sdk/typescript 0.18.2 2.438.15 1.0.51 unstructured-client", + "speakeasy-sdk/typescript 0.19.0 2.493.13 1.0.58 unstructured-client", } as const; diff --git a/src/lib/encodings.ts b/src/lib/encodings.ts index 44fa728..25c9dcb 100644 --- a/src/lib/encodings.ts +++ b/src/lib/encodings.ts @@ -16,12 +16,16 @@ export function encodeMatrix( key: string, value: unknown, options?: { explode?: boolean; charEncoding?: "percent" | "none" }, -): string { +): string | undefined { let out = ""; const pairs: [string, unknown][] = options?.explode ? explode(key, value) : [[key, value]]; + if (pairs.every(([_, v]) => v == null)) { + return; + } + const encodeString = (v: string) => { return options?.charEncoding === "percent" ? encodeURIComponent(v) : v; }; @@ -29,21 +33,25 @@ export function encodeMatrix( pairs.forEach(([pk, pv]) => { let tmp = ""; - let encValue = ""; + let encValue: string | null | undefined = null; - if (pv === undefined) { + if (pv == null) { return; } else if (Array.isArray(pv)) { - encValue = mapDefined(pv, (v) => `${encodeValue(v)}`).join(","); + encValue = mapDefined(pv, (v) => `${encodeValue(v)}`)?.join(","); } else if (isPlainObject(pv)) { - encValue = mapDefinedEntries(Object.entries(pv), ([k, v]) => { + const mapped = mapDefinedEntries(Object.entries(pv), ([k, v]) => { return `,${encodeString(k)},${encodeValue(v)}`; - }).join(""); - encValue = encValue.slice(1); + }); + encValue = mapped?.join("").slice(1); } else { encValue = `${encodeValue(pv)}`; } + if (encValue == null) { + return; + } + const keyPrefix = encodeString(pk); tmp = `${keyPrefix}=${encValue}`; // trim trailing '=' if value was empty @@ -66,36 +74,40 @@ export function encodeLabel( key: string, value: unknown, options?: { explode?: boolean; charEncoding?: "percent" | "none" }, -): string { +): string | undefined { let out = ""; const pairs: [string, unknown][] = options?.explode ? explode(key, value) : [[key, value]]; + if (pairs.every(([_, v]) => v == null)) { + return; + } + const encodeString = (v: string) => { return options?.charEncoding === "percent" ? encodeURIComponent(v) : v; }; const encodeValue = (v: unknown) => encodeString(serializeValue(v)); pairs.forEach(([pk, pv]) => { - let encValue = ""; + let encValue: string | null | undefined = ""; - if (pv === undefined) { + if (pv == null) { return; } else if (Array.isArray(pv)) { - encValue = mapDefined(pv, (v) => `${encodeValue(v)}`).join("."); + encValue = mapDefined(pv, (v) => `${encodeValue(v)}`)?.join("."); } else if (isPlainObject(pv)) { - encValue = mapDefinedEntries(Object.entries(pv), ([k, v]) => { + const mapped = mapDefinedEntries(Object.entries(pv), ([k, v]) => { return `.${encodeString(k)}.${encodeValue(v)}`; - }).join(""); - encValue = encValue.slice(1); + }); + encValue = mapped?.join("").slice(1); } else { const k = options?.explode && isPlainObject(value) ? `${encodeString(pk)}=` : ""; encValue = `${k}${encodeValue(pv)}`; } - out += `.${encValue}`; + out += encValue == null ? "" : `.${encValue}`; }); return out; @@ -105,7 +117,7 @@ type FormEncoder = ( key: string, value: unknown, options?: { explode?: boolean; charEncoding?: "percent" | "none" }, -) => string; +) => string | undefined; function formEncoder(sep: string): FormEncoder { return ( @@ -118,6 +130,10 @@ function formEncoder(sep: string): FormEncoder { ? explode(key, value) : [[key, value]]; + if (pairs.every(([_, v]) => v == null)) { + return; + } + const encodeString = (v: string) => { return options?.charEncoding === "percent" ? encodeURIComponent(v) : v; }; @@ -128,20 +144,24 @@ function formEncoder(sep: string): FormEncoder { pairs.forEach(([pk, pv]) => { let tmp = ""; - let encValue = ""; + let encValue: string | null | undefined = null; - if (pv === undefined) { + if (pv == null) { return; } else if (Array.isArray(pv)) { - encValue = mapDefined(pv, (v) => `${encodeValue(v)}`).join(encodedSep); + encValue = mapDefined(pv, (v) => `${encodeValue(v)}`)?.join(encodedSep); } else if (isPlainObject(pv)) { encValue = mapDefinedEntries(Object.entries(pv), ([k, v]) => { return `${encodeString(k)}${encodedSep}${encodeValue(v)}`; - }).join(encodedSep); + })?.join(encodedSep); } else { encValue = `${encodeValue(pv)}`; } + if (encValue == null) { + return; + } + tmp = `${encodeString(pk)}=${encValue}`; // If we end up with the nothing then skip forward @@ -180,7 +200,7 @@ export function encodeBodyForm( let tmp = ""; let encValue = ""; - if (pv === undefined) { + if (pv == null) { return; } else if (Array.isArray(pv)) { encValue = JSON.stringify(pv, jsonReplacer); @@ -207,14 +227,14 @@ export function encodeDeepObject( key: string, value: unknown, options?: { charEncoding?: "percent" | "none" }, -): string { +): string | undefined { if (value == null) { - return ""; + return; } if (!isPlainObject(value)) { throw new EncodingError( - `Value of parameter '${key}' which uses deepObject encoding must be an object`, + `Value of parameter '${key}' which uses deepObject encoding must be an object or null`, ); } @@ -225,9 +245,9 @@ export function encodeDeepObjectObject( key: string, value: unknown, options?: { charEncoding?: "percent" | "none" }, -): string { +): string | undefined { if (value == null) { - return ""; + return; } let out = ""; @@ -241,7 +261,7 @@ export function encodeDeepObjectObject( } Object.entries(value).forEach(([ck, cv]) => { - if (cv === undefined) { + if (cv == null) { return; } @@ -250,19 +270,17 @@ export function encodeDeepObjectObject( if (isPlainObject(cv)) { const objOut = encodeDeepObjectObject(pk, cv, options); - out += `&${objOut}`; + out += objOut == null ? "" : `&${objOut}`; return; } const pairs: unknown[] = Array.isArray(cv) ? cv : [cv]; - let encoded = ""; - - encoded = mapDefined(pairs, (v) => { + const encoded = mapDefined(pairs, (v) => { return `${encodeString(pk)}=${encodeString(serializeValue(v))}`; - }).join("&"); + })?.join("&"); - out += `&${encoded}`; + out += encoded == null ? "" : `&${encoded}`; }); return out.slice(1); @@ -272,9 +290,9 @@ export function encodeJSON( key: string, value: unknown, options?: { explode?: boolean; charEncoding?: "percent" | "none" }, -): string { +): string | undefined { if (typeof value === "undefined") { - return ""; + return; } const encodeString = (v: string) => { @@ -290,40 +308,39 @@ export const encodeSimple = ( key: string, value: unknown, options?: { explode?: boolean; charEncoding?: "percent" | "none" }, -): string => { +): string | undefined => { let out = ""; const pairs: [string, unknown][] = options?.explode ? explode(key, value) : [[key, value]]; + if (pairs.every(([_, v]) => v == null)) { + return; + } + const encodeString = (v: string) => { return options?.charEncoding === "percent" ? encodeURIComponent(v) : v; }; const encodeValue = (v: unknown) => encodeString(serializeValue(v)); pairs.forEach(([pk, pv]) => { - let tmp = ""; + let tmp: string | null | undefined = ""; - if (pv === undefined) { + if (pv == null) { return; } else if (Array.isArray(pv)) { - tmp = mapDefined(pv, (v) => `${encodeValue(v)}`).join(","); + tmp = mapDefined(pv, (v) => `${encodeValue(v)}`)?.join(","); } else if (isPlainObject(pv)) { - tmp = mapDefinedEntries(Object.entries(pv), ([k, v]) => { + const mapped = mapDefinedEntries(Object.entries(pv), ([k, v]) => { return `,${encodeString(k)},${encodeValue(v)}`; - }).join(""); - tmp = tmp.slice(1); + }); + tmp = mapped?.join("").slice(1); } else { const k = options?.explode && isPlainObject(value) ? `${pk}=` : ""; tmp = `${k}${encodeValue(pv)}`; } - // If we end up with the nothing then skip forward - if (!tmp) { - return; - } - - out += `,${tmp}`; + out += tmp ? `,${tmp}` : ""; }); return out.slice(1); @@ -341,9 +358,7 @@ function explode(key: string, value: unknown): [string, unknown][] { } function serializeValue(value: unknown): string { - if (value === null) { - return "null"; - } else if (typeof value === "undefined") { + if (value == null) { return ""; } else if (value instanceof Date) { return value.toISOString(); @@ -364,14 +379,14 @@ function jsonReplacer(_: string, value: unknown): unknown { } } -function mapDefined(inp: T[], mapper: (v: T) => R): R[] { - return inp.reduce((acc, v) => { - if (v === undefined) { +function mapDefined(inp: T[], mapper: (v: T) => R): R[] | null { + const res = inp.reduce((acc, v) => { + if (v == null) { return acc; } const m = mapper(v); - if (m === undefined) { + if (m == null) { return acc; } @@ -379,30 +394,32 @@ function mapDefined(inp: T[], mapper: (v: T) => R): R[] { return acc; }, []); + + return res.length ? res : null; } function mapDefinedEntries( inp: Iterable<[K, V]>, mapper: (v: [K, V]) => R, -): R[] { +): R[] | null { const acc: R[] = []; for (const [k, v] of inp) { - if (v === undefined) { + if (v == null) { continue; } const m = mapper([k, v]); - if (m === undefined) { + if (m == null) { continue; } acc.push(m); } - return acc; + return acc.length ? acc : null; } -export function queryJoin(...args: string[]): string { +export function queryJoin(...args: (string | undefined)[]): string { return args.filter(Boolean).join("&"); } @@ -415,7 +432,7 @@ type QueryEncoder = ( key: string, value: unknown, options?: QueryEncoderOptions, -) => string; +) => string | undefined; type BulkQueryEncoder = ( values: Record, @@ -447,3 +464,20 @@ export const encodeFormQuery = queryEncoder(encodeForm); export const encodeSpaceDelimitedQuery = queryEncoder(encodeSpaceDelimited); export const encodePipeDelimitedQuery = queryEncoder(encodePipeDelimited); export const encodeDeepObjectQuery = queryEncoder(encodeDeepObject); + +export function appendForm( + fd: FormData, + key: string, + value: unknown, + fileName?: string, +): void { + if (value == null) { + return; + } else if (value instanceof Blob && fileName) { + fd.append(key, value, fileName); + } else if (value instanceof Blob) { + fd.append(key, value); + } else { + fd.append(key, String(value)); + } +} diff --git a/src/lib/matchers.ts b/src/lib/matchers.ts index 1ff641e..c538ea2 100644 --- a/src/lib/matchers.ts +++ b/src/lib/matchers.ts @@ -205,7 +205,7 @@ export function match( raw = await response.json(); break; case "bytes": - raw = await response.arrayBuffer(); + raw = new Uint8Array(await response.arrayBuffer()); break; case "stream": raw = response.body; diff --git a/src/lib/primitives.ts b/src/lib/primitives.ts index 23794a2..ce50b98 100644 --- a/src/lib/primitives.ts +++ b/src/lib/primitives.ts @@ -2,6 +2,26 @@ * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT. */ +class InvariantError extends Error { + constructor(message: string) { + super(message); + this.name = "InvariantError"; + } +} + +export function invariant( + condition: unknown, + message: string, +): asserts condition { + if (!condition) { + throw new InvariantError(message); + } +} + +export type ExactPartial = { + [P in keyof T]?: T[P] | undefined; +}; + export type Remap = { [k in keyof Inp as Mapping[k] extends string /* if we have a string mapping for this key then use it */ ? Mapping[k] @@ -38,3 +58,79 @@ export function remap< return out; } + +export function combineSignals( + ...signals: Array +): AbortSignal | null { + const filtered: AbortSignal[] = []; + for (const signal of signals) { + if (signal) { + filtered.push(signal); + } + } + + switch (filtered.length) { + case 0: + case 1: + return filtered[0] || null; + default: + if ("any" in AbortSignal && typeof AbortSignal.any === "function") { + return AbortSignal.any(filtered); + } + return abortSignalAny(filtered); + } +} + +export function abortSignalAny(signals: AbortSignal[]): AbortSignal { + const controller = new AbortController(); + const result = controller.signal; + if (!signals.length) { + return controller.signal; + } + + if (signals.length === 1) { + return signals[0] || controller.signal; + } + + for (const signal of signals) { + if (signal.aborted) { + return signal; + } + } + + function abort(this: AbortSignal) { + controller.abort(this.reason); + clean(); + } + + const signalRefs: WeakRef[] = []; + function clean() { + for (const signalRef of signalRefs) { + const signal = signalRef.deref(); + if (signal) { + signal.removeEventListener("abort", abort); + } + } + } + + for (const signal of signals) { + signalRefs.push(new WeakRef(signal)); + signal.addEventListener("abort", abort); + } + + return result; +} + +export function compactMap( + values: Record, +): Record { + const out: Record = {}; + + for (const [k, v] of Object.entries(values)) { + if (typeof v !== "undefined") { + out[k] = v; + } + } + + return out; +} diff --git a/src/lib/retries.ts b/src/lib/retries.ts index df3e0bc..93ebc8d 100644 --- a/src/lib/retries.ts +++ b/src/lib/retries.ts @@ -26,23 +26,44 @@ export type RetryConfig = retryConnectionErrors?: boolean; }; -class PermanentError extends Error { - inner: unknown; +/** + * PermanentError is an error that is not recoverable. Throwing this error will + * cause a retry loop to terminate. + */ +export class PermanentError extends Error { + /** The underlying cause of the error. */ + override readonly cause: unknown; + + constructor(message: string, options?: { cause?: unknown }) { + let msg = message; + if (options?.cause) { + msg += `: ${options.cause}`; + } - constructor(inner: unknown) { - super("Permanent error"); - this.inner = inner; + super(msg, options); + this.name = "PermanentError"; + // In older runtimes, the cause field would not have been assigned through + // the super() call. + if (typeof this.cause === "undefined") { + this.cause = options?.cause; + } Object.setPrototypeOf(this, PermanentError.prototype); } } -class TemporaryError extends Error { - res: Response; +/** + * TemporaryError is an error is used to signal that an HTTP request can be + * retried as part of a retry loop. If retry attempts are exhausted and this + * error is thrown, the response will be returned to the caller. + */ +export class TemporaryError extends Error { + response: Response; - constructor(res: Response) { - super("Temporary error"); - this.res = res; + constructor(message: string, response: Response) { + super(message); + this.response = response; + this.name = "TemporaryError"; Object.setPrototypeOf(this, TemporaryError.prototype); } @@ -80,11 +101,14 @@ function wrapFetcher( try { const res = await fn(); if (isRetryableResponse(res, options.statusCodes)) { - throw new TemporaryError(res); + throw new TemporaryError( + "Response failed with retryable status code", + res, + ); } return res; - } catch (err) { + } catch (err: unknown) { if (err instanceof TemporaryError) { throw err; } @@ -96,7 +120,7 @@ function wrapFetcher( throw err; } - throw new PermanentError(err); + throw new PermanentError("Permanent error", { cause: err }); } }; } @@ -139,37 +163,25 @@ async function retryBackoff( try { const res = await fn(); return res; - } catch (err) { + } catch (err: unknown) { if (err instanceof PermanentError) { - throw err.inner; + throw err.cause; } const elapsed = Date.now() - start; if (elapsed > maxElapsedTime) { if (err instanceof TemporaryError) { - return err.res; + return err.response; } throw err; } let retryInterval = 0; - if (err instanceof TemporaryError && err.res && err.res.headers) { - const retryVal = err.res.headers.get("retry-after") || ""; - if (retryVal != "") { - const parsedNumber = Number(retryVal); - if (!isNaN(parsedNumber) && Number.isInteger(parsedNumber)) { - retryInterval = parsedNumber * 1000; - } else { - const parsedDate = Date.parse(retryVal); - if (!isNaN(parsedDate)) { - const deltaMS = parsedDate - Date.now(); - retryInterval = deltaMS > 0 ? Math.ceil(deltaMS) : 0; - } - } - } + if (err instanceof TemporaryError) { + retryInterval = retryIntervalFromResponse(err.response); } - if (retryInterval == 0) { + if (retryInterval <= 0) { retryInterval = initialInterval * Math.pow(x, exponent) + Math.random() * 1000; } @@ -182,6 +194,26 @@ async function retryBackoff( } } +function retryIntervalFromResponse(res: Response): number { + const retryVal = res.headers.get("retry-after") || ""; + if (!retryVal) { + return 0; + } + + const parsedNumber = Number(retryVal); + if (Number.isInteger(parsedNumber)) { + return parsedNumber * 1000; + } + + const parsedDate = Date.parse(retryVal); + if (Number.isInteger(parsedDate)) { + const deltaMS = parsedDate - Date.now(); + return deltaMS > 0 ? Math.ceil(deltaMS) : 0; + } + + return 0; +} + async function delay(delay: number): Promise { return new Promise((resolve) => setTimeout(resolve, delay)); } diff --git a/src/lib/sdks.ts b/src/lib/sdks.ts index bfbe7a5..8bfac63 100644 --- a/src/lib/sdks.ts +++ b/src/lib/sdks.ts @@ -41,6 +41,10 @@ export type RequestOptions = { * Specifies the status codes which should be retried using the given retry policy. */ retryCodes?: string[]; + /** + * Overrides the base server URL that will be used by an operation. + */ + serverURL?: string | URL; /** * Sets various request options on the `fetch` call made by an SDK method. * @@ -52,7 +56,7 @@ export type RequestOptions = { type RequestConfig = { method: string; path: string; - baseURL?: string | URL; + baseURL?: string | URL | undefined; query?: string; body?: RequestInit["body"]; headers?: HeadersInit; @@ -119,6 +123,7 @@ export class ClientSDK { const inputURL = new URL(path, reqURL); if (path) { + reqURL.pathname += reqURL.pathname.endsWith("/") ? "" : "/"; reqURL.pathname += inputURL.pathname.replace(/^\/+/, ""); } @@ -126,7 +131,10 @@ export class ClientSDK { const secQuery: string[] = []; for (const [k, v] of Object.entries(security?.queryParams || {})) { - secQuery.push(encodeForm(k, v, { charEncoding: "percent" })); + const q = encodeForm(k, v, { charEncoding: "percent" }); + if (typeof q !== "undefined") { + secQuery.push(q); + } } if (secQuery.length) { finalQuery += `&${secQuery.join("&")}`; @@ -220,8 +228,8 @@ export class ClientSDK { options: { context: HookContext; errorCodes: number | string | (number | string)[]; - retryConfig?: RetryConfig | undefined; - retryCodes?: string[] | undefined; + retryConfig: RetryConfig; + retryCodes: string[]; }, ): Promise< Result< @@ -233,8 +241,6 @@ export class ClientSDK { > > { const { context, errorCodes } = options; - const retryConfig = options.retryConfig || { strategy: "none" }; - const retryCodes = options.retryCodes || []; return retry( async () => { @@ -245,22 +251,28 @@ export class ClientSDK { let response = await this.#httpClient.request(req); - if (matchStatusCode(response, errorCodes)) { - const result = await this.#hooks.afterError(context, response, null); - if (result.error) { - throw result.error; + try { + if (matchStatusCode(response, errorCodes)) { + const result = await this.#hooks.afterError( + context, + response, + null, + ); + if (result.error) { + throw result.error; + } + response = result.response || response; + } else { + response = await this.#hooks.afterSuccess(context, response); } - response = result.response || response; - } else { - response = await this.#hooks.afterSuccess(context, response); + } finally { + await logResponse(this.#logger, response, req) + .catch(e => this.#logger?.log("Failed to log response:", e)); } - await logResponse(this.#logger, response, req) - .catch(e => this.#logger?.log("Failed to log response:", e)); - return response; }, - { config: retryConfig, statusCodes: retryCodes }, + { config: options.retryConfig, statusCodes: options.retryCodes }, ).then( (r) => OK(r), (err) => { diff --git a/src/lib/security.ts b/src/lib/security.ts index b53a900..58a35cd 100644 --- a/src/lib/security.ts +++ b/src/lib/security.ts @@ -4,6 +4,14 @@ import * as shared from "../sdk/models/shared/index.js"; +type OAuth2PasswordFlow = { + username: string; + password?: string | undefined; + clientID: string; + clientSecret?: string | undefined; + tokenURL: string; +}; + export enum SecurityErrorCode { Incomplete = "incomplete", UnrecognisedSecurityType = "unrecognized_security_type", @@ -37,6 +45,7 @@ export type SecurityState = { headers: Record; queryParams: Record; cookies: Record; + oauth2: ({ type: "password" } & OAuth2PasswordFlow) | { type: "none" }; }; type SecurityInputBasic = { @@ -73,7 +82,18 @@ type SecurityInputOAuth2 = { type SecurityInputOAuth2ClientCredentials = { type: "oauth2:client_credentials"; - value: string | null | undefined; + value: + | { clientID?: string | undefined; clientSecret?: string | undefined } + | null + | undefined; +}; + +type SecurityInputOAuth2PasswordCredentials = { + type: "oauth2:password"; + value: + | string + | null + | undefined; fieldName: string; }; @@ -89,6 +109,7 @@ export type SecurityInput = | SecurityInputAPIKey | SecurityInputOAuth2 | SecurityInputOAuth2ClientCredentials + | SecurityInputOAuth2PasswordCredentials | SecurityInputOIDC | SecurityInputCustom; @@ -96,10 +117,11 @@ export function resolveSecurity( ...options: SecurityInput[][] ): SecurityState | null { const state: SecurityState = { - basic: { username: "", password: "" }, + basic: {}, headers: {}, queryParams: {}, cookies: {}, + oauth2: { type: "none" }, }; const option = options.find((opts) => { @@ -110,6 +132,12 @@ export function resolveSecurity( return o.value.username != null || o.value.password != null; } else if (o.type === "http:custom") { return null; + } else if (o.type === "oauth2:password") { + return ( + typeof o.value === "string" && !!o.value + ); + } else if (o.type === "oauth2:client_credentials") { + return o.value.clientID != null || o.value.clientSecret != null; } else if (typeof o.value === "string") { return !!o.value; } else { @@ -152,6 +180,9 @@ export function resolveSecurity( case "oauth2": applyBearer(state, spec); break; + case "oauth2:password": + applyBearer(state, spec); + break; case "oauth2:client_credentials": break; case "openIdConnect": @@ -179,9 +210,13 @@ function applyBasic( function applyBearer( state: SecurityState, - spec: SecurityInputBearer | SecurityInputOAuth2 | SecurityInputOIDC, + spec: + | SecurityInputBearer + | SecurityInputOAuth2 + | SecurityInputOIDC + | SecurityInputOAuth2PasswordCredentials, ) { - if (spec.value == null) { + if (typeof spec.value !== "string" || !spec.value) { return; } @@ -192,6 +227,7 @@ function applyBearer( state.headers[spec.fieldName] = value; } + export function resolveGlobalSecurity( security: Partial | null | undefined, ): SecurityState | null { diff --git a/src/sdk/general.ts b/src/sdk/general.ts index 041b107..917773f 100644 --- a/src/sdk/general.ts +++ b/src/sdk/general.ts @@ -2,11 +2,16 @@ * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT. */ -import { generalPartition } from "../funcs/generalPartition.js"; +import { + generalPartition, + PartitionAcceptEnum, +} from "../funcs/generalPartition.js"; import { ClientSDK, RequestOptions } from "../lib/sdks.js"; import * as operations from "./models/operations/index.js"; import { unwrapAsync } from "./types/fp.js"; +export { PartitionAcceptEnum } from "../funcs/generalPartition.js"; + export class General extends ClientSDK { /** * Summary @@ -16,7 +21,7 @@ export class General extends ClientSDK { */ async partition( request: operations.PartitionRequest, - options?: RequestOptions, + options?: RequestOptions & { acceptHeaderOverride?: PartitionAcceptEnum }, ): Promise { return unwrapAsync(generalPartition( this, diff --git a/src/sdk/models/errors/httpvalidationerror.ts b/src/sdk/models/errors/httpvalidationerror.ts index b9cc0c8..089d646 100644 --- a/src/sdk/models/errors/httpvalidationerror.ts +++ b/src/sdk/models/errors/httpvalidationerror.ts @@ -3,7 +3,10 @@ */ import * as z from "zod"; +import { safeParse } from "../../../lib/schemas.js"; +import { Result as SafeParseResult } from "../../types/fp.js"; import * as shared from "../shared/index.js"; +import { SDKValidationError } from "./sdkvalidationerror.js"; export type Detail = Array | string; @@ -57,6 +60,20 @@ export namespace Detail$ { export type Outbound = Detail$Outbound; } +export function detailToJSON(detail: Detail): string { + return JSON.stringify(Detail$outboundSchema.parse(detail)); +} + +export function detailFromJSON( + jsonString: string, +): SafeParseResult { + return safeParse( + jsonString, + (x) => Detail$inboundSchema.parse(JSON.parse(x)), + `Failed to parse 'Detail' from JSON`, + ); +} + /** @internal */ export const HTTPValidationError$inboundSchema: z.ZodType< HTTPValidationError, diff --git a/src/sdk/models/operations/partition.ts b/src/sdk/models/operations/partition.ts index 910b990..e2e6d5b 100644 --- a/src/sdk/models/operations/partition.ts +++ b/src/sdk/models/operations/partition.ts @@ -4,6 +4,9 @@ import * as z from "zod"; import { remap as remap$ } from "../../../lib/primitives.js"; +import { safeParse } from "../../../lib/schemas.js"; +import { Result as SafeParseResult } from "../../types/fp.js"; +import { SDKValidationError } from "../errors/sdkvalidationerror.js"; import * as shared from "../shared/index.js"; export type PartitionRequest = { @@ -16,10 +19,6 @@ export type PartitionResponse = { * HTTP response content type for this operation */ contentType: string; - /** - * Successful Response - */ - elements?: Array<{ [k: string]: any }> | undefined; /** * HTTP response status code for this operation */ @@ -28,6 +27,14 @@ export type PartitionResponse = { * Raw HTTP response; suitable for custom response parsing */ rawResponse: Response; + /** + * Successful Response + */ + csvElements?: string | undefined; + /** + * Successful Response + */ + elements?: Array<{ [k: string]: any }> | undefined; }; /** @internal */ @@ -79,6 +86,24 @@ export namespace PartitionRequest$ { export type Outbound = PartitionRequest$Outbound; } +export function partitionRequestToJSON( + partitionRequest: PartitionRequest, +): string { + return JSON.stringify( + PartitionRequest$outboundSchema.parse(partitionRequest), + ); +} + +export function partitionRequestFromJSON( + jsonString: string, +): SafeParseResult { + return safeParse( + jsonString, + (x) => PartitionRequest$inboundSchema.parse(JSON.parse(x)), + `Failed to parse 'PartitionRequest' from JSON`, + ); +} + /** @internal */ export const PartitionResponse$inboundSchema: z.ZodType< PartitionResponse, @@ -86,24 +111,26 @@ export const PartitionResponse$inboundSchema: z.ZodType< unknown > = z.object({ ContentType: z.string(), - Elements: z.array(z.record(z.any())).optional(), StatusCode: z.number().int(), RawResponse: z.instanceof(Response), + csv_elements: z.string().optional(), + elements: z.array(z.record(z.any())).optional(), }).transform((v) => { return remap$(v, { "ContentType": "contentType", - "Elements": "elements", "StatusCode": "statusCode", "RawResponse": "rawResponse", + "csv_elements": "csvElements", }); }); /** @internal */ export type PartitionResponse$Outbound = { ContentType: string; - Elements?: Array<{ [k: string]: any }> | undefined; StatusCode: number; RawResponse: never; + csv_elements?: string | undefined; + elements?: Array<{ [k: string]: any }> | undefined; }; /** @internal */ @@ -113,17 +140,18 @@ export const PartitionResponse$outboundSchema: z.ZodType< PartitionResponse > = z.object({ contentType: z.string(), - elements: z.array(z.record(z.any())).optional(), statusCode: z.number().int(), rawResponse: z.instanceof(Response).transform(() => { throw new Error("Response cannot be serialized"); }), + csvElements: z.string().optional(), + elements: z.array(z.record(z.any())).optional(), }).transform((v) => { return remap$(v, { contentType: "ContentType", - elements: "Elements", statusCode: "StatusCode", rawResponse: "RawResponse", + csvElements: "csv_elements", }); }); @@ -139,3 +167,21 @@ export namespace PartitionResponse$ { /** @deprecated use `PartitionResponse$Outbound` instead. */ export type Outbound = PartitionResponse$Outbound; } + +export function partitionResponseToJSON( + partitionResponse: PartitionResponse, +): string { + return JSON.stringify( + PartitionResponse$outboundSchema.parse(partitionResponse), + ); +} + +export function partitionResponseFromJSON( + jsonString: string, +): SafeParseResult { + return safeParse( + jsonString, + (x) => PartitionResponse$inboundSchema.parse(JSON.parse(x)), + `Failed to parse 'PartitionResponse' from JSON`, + ); +} diff --git a/src/sdk/models/shared/partitionparameters.ts b/src/sdk/models/shared/partitionparameters.ts index 2163d78..cae39a1 100644 --- a/src/sdk/models/shared/partitionparameters.ts +++ b/src/sdk/models/shared/partitionparameters.ts @@ -4,12 +4,15 @@ import * as z from "zod"; import { remap as remap$ } from "../../../lib/primitives.js"; +import { safeParse } from "../../../lib/schemas.js"; import { blobLikeSchema } from "../../types/blobs.js"; import { catchUnrecognizedEnum, OpenEnum, Unrecognized, } from "../../types/enums.js"; +import { Result as SafeParseResult } from "../../types/fp.js"; +import { SDKValidationError } from "../errors/sdkvalidationerror.js"; export enum ChunkingStrategy { Basic = "basic", @@ -44,6 +47,7 @@ export enum Strategy { HiRes = "hi_res", Auto = "auto", OcrOnly = "ocr_only", + OdOnly = "od_only", } /** * The strategy to use for partitioning PDF/image. Options are fast, hi_res, auto. Default: hi_res @@ -167,6 +171,10 @@ export type PartitionParameters = { * The strategy to use for partitioning PDF/image. Options are fast, hi_res, auto. Default: hi_res */ strategy?: StrategyOpen | undefined; + /** + * The OCR agent to use for table ocr inference. + */ + tableOcrAgent?: string | null | undefined; /** * When `True`, assign UUIDs to element IDs, which guarantees their uniqueness (useful when using them as primary keys in database). Otherwise a SHA-256 of element text is used. Default: `False` */ @@ -255,6 +263,20 @@ export namespace Files$ { export type Outbound = Files$Outbound; } +export function filesToJSON(files: Files): string { + return JSON.stringify(Files$outboundSchema.parse(files)); +} + +export function filesFromJSON( + jsonString: string, +): SafeParseResult { + return safeParse( + jsonString, + (x) => Files$inboundSchema.parse(JSON.parse(x)), + `Failed to parse 'Files' from JSON`, + ); +} + /** @internal */ export const OutputFormat$inboundSchema: z.ZodType< OutputFormatOpen, @@ -356,6 +378,7 @@ export const PartitionParameters$inboundSchema: z.ZodType< split_pdf_page_range: z.array(z.number().int()).optional(), starting_page_number: z.nullable(z.number().int()).optional(), strategy: Strategy$inboundSchema.default(Strategy.HiRes), + table_ocr_agent: z.nullable(z.string()).optional(), unique_element_ids: z.boolean().default(false), xml_keep_tags: z.boolean().default(false), }).transform((v) => { @@ -383,6 +406,7 @@ export const PartitionParameters$inboundSchema: z.ZodType< "split_pdf_page": "splitPdfPage", "split_pdf_page_range": "splitPdfPageRange", "starting_page_number": "startingPageNumber", + "table_ocr_agent": "tableOcrAgent", "unique_element_ids": "uniqueElementIds", "xml_keep_tags": "xmlKeepTags", }); @@ -419,6 +443,7 @@ export type PartitionParameters$Outbound = { split_pdf_page_range?: Array | undefined; starting_page_number?: number | null | undefined; strategy: string; + table_ocr_agent?: string | null | undefined; unique_element_ids: boolean; xml_keep_tags: boolean; }; @@ -460,6 +485,7 @@ export const PartitionParameters$outboundSchema: z.ZodType< splitPdfPageRange: z.array(z.number().int()).optional(), startingPageNumber: z.nullable(z.number().int()).optional(), strategy: Strategy$outboundSchema.default(Strategy.HiRes), + tableOcrAgent: z.nullable(z.string()).optional(), uniqueElementIds: z.boolean().default(false), xmlKeepTags: z.boolean().default(false), }).transform((v) => { @@ -487,6 +513,7 @@ export const PartitionParameters$outboundSchema: z.ZodType< splitPdfPage: "split_pdf_page", splitPdfPageRange: "split_pdf_page_range", startingPageNumber: "starting_page_number", + tableOcrAgent: "table_ocr_agent", uniqueElementIds: "unique_element_ids", xmlKeepTags: "xml_keep_tags", }); @@ -504,3 +531,21 @@ export namespace PartitionParameters$ { /** @deprecated use `PartitionParameters$Outbound` instead. */ export type Outbound = PartitionParameters$Outbound; } + +export function partitionParametersToJSON( + partitionParameters: PartitionParameters, +): string { + return JSON.stringify( + PartitionParameters$outboundSchema.parse(partitionParameters), + ); +} + +export function partitionParametersFromJSON( + jsonString: string, +): SafeParseResult { + return safeParse( + jsonString, + (x) => PartitionParameters$inboundSchema.parse(JSON.parse(x)), + `Failed to parse 'PartitionParameters' from JSON`, + ); +} diff --git a/src/sdk/models/shared/security.ts b/src/sdk/models/shared/security.ts index 1e7132f..c7006de 100644 --- a/src/sdk/models/shared/security.ts +++ b/src/sdk/models/shared/security.ts @@ -4,6 +4,9 @@ import * as z from "zod"; import { remap as remap$ } from "../../../lib/primitives.js"; +import { safeParse } from "../../../lib/schemas.js"; +import { Result as SafeParseResult } from "../../types/fp.js"; +import { SDKValidationError } from "../errors/sdkvalidationerror.js"; export type Security = { apiKeyAuth?: string | undefined; @@ -52,3 +55,17 @@ export namespace Security$ { /** @deprecated use `Security$Outbound` instead. */ export type Outbound = Security$Outbound; } + +export function securityToJSON(security: Security): string { + return JSON.stringify(Security$outboundSchema.parse(security)); +} + +export function securityFromJSON( + jsonString: string, +): SafeParseResult { + return safeParse( + jsonString, + (x) => Security$inboundSchema.parse(JSON.parse(x)), + `Failed to parse 'Security' from JSON`, + ); +} diff --git a/src/sdk/models/shared/validationerror.ts b/src/sdk/models/shared/validationerror.ts index bfe2a68..460f9c9 100644 --- a/src/sdk/models/shared/validationerror.ts +++ b/src/sdk/models/shared/validationerror.ts @@ -3,6 +3,9 @@ */ import * as z from "zod"; +import { safeParse } from "../../../lib/schemas.js"; +import { Result as SafeParseResult } from "../../types/fp.js"; +import { SDKValidationError } from "../errors/sdkvalidationerror.js"; export type Loc = string | number; @@ -37,6 +40,20 @@ export namespace Loc$ { export type Outbound = Loc$Outbound; } +export function locToJSON(loc: Loc): string { + return JSON.stringify(Loc$outboundSchema.parse(loc)); +} + +export function locFromJSON( + jsonString: string, +): SafeParseResult { + return safeParse( + jsonString, + (x) => Loc$inboundSchema.parse(JSON.parse(x)), + `Failed to parse 'Loc' from JSON`, + ); +} + /** @internal */ export const ValidationError$inboundSchema: z.ZodType< ValidationError, @@ -78,3 +95,19 @@ export namespace ValidationError$ { /** @deprecated use `ValidationError$Outbound` instead. */ export type Outbound = ValidationError$Outbound; } + +export function validationErrorToJSON( + validationError: ValidationError, +): string { + return JSON.stringify(ValidationError$outboundSchema.parse(validationError)); +} + +export function validationErrorFromJSON( + jsonString: string, +): SafeParseResult { + return safeParse( + jsonString, + (x) => ValidationError$inboundSchema.parse(JSON.parse(x)), + `Failed to parse 'ValidationError' from JSON`, + ); +} diff --git a/src/sdk/types/operations.ts b/src/sdk/types/operations.ts index 0952f6f..beb81e1 100644 --- a/src/sdk/types/operations.ts +++ b/src/sdk/types/operations.ts @@ -6,9 +6,10 @@ import { Result } from "./fp.js"; export type Paginator = () => Promise }> | null; -export type PageIterator = V & { +export type PageIterator = V & { next: Paginator; [Symbol.asyncIterator]: () => AsyncIterableIterator; + "~next"?: PageState | undefined; }; export function createPageIterator( @@ -40,7 +41,9 @@ export function createPageIterator( * terminates. It is useful in paginated SDK functions that have early return * paths when things go wrong. */ -export function haltIterator(v: V): PageIterator { +export function haltIterator( + v: V, +): PageIterator { return { ...v, next: () => null, @@ -54,9 +57,9 @@ export function haltIterator(v: V): PageIterator { * Converts an async iterator of `Result` into an async iterator of `V`. * When error results occur, the underlying error value is thrown. */ -export async function unwrapResultIterator( - iteratorPromise: Promise>>, -): Promise> { +export async function unwrapResultIterator( + iteratorPromise: Promise, PageState>>, +): Promise> { const resultIter = await iteratorPromise; if (!resultIter.ok) { @@ -66,6 +69,7 @@ export async function unwrapResultIterator( return { ...resultIter.value, next: unwrapPaginator(resultIter.next), + "~next": resultIter["~next"], [Symbol.asyncIterator]: async function* paginator() { for await (const page of resultIter) { if (!page.ok) { @@ -97,3 +101,5 @@ function unwrapPaginator( }); }; } + +export const URL_OVERRIDE = Symbol("URL_OVERRIDE"); diff --git a/tsconfig.json b/tsconfig.json index 7a86d2d..94d81a3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,8 +2,9 @@ "compilerOptions": { "incremental": true, "tsBuildInfoFile": ".tsbuildinfo", - "target": "ES2018", + "target": "ES2020", "lib": ["ES2022", "DOM", "DOM.Iterable"], + "jsx": "react-jsx", "module": "Node16", "moduleResolution": "Node16",