@puku-ai/sdk 4.0.3 → 4.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -7
- package/dist/index.cjs +85 -0
- package/dist/vendor/_vendor/partial-json-parser/parser.cjs +225 -0
- package/dist/vendor/api-promise.cjs +18 -0
- package/dist/vendor/client.cjs +1013 -0
- package/dist/vendor/core/api-promise.cjs +80 -0
- package/dist/vendor/core/api.cjs +2 -0
- package/dist/vendor/core/credentials.cjs +313 -0
- package/dist/vendor/core/error.cjs +156 -0
- package/dist/vendor/core/middleware.cjs +173 -0
- package/dist/vendor/core/pagination.cjs +215 -0
- package/dist/vendor/core/resource.cjs +11 -0
- package/dist/vendor/core/streaming.cjs +346 -0
- package/dist/vendor/core/uploads.cjs +5 -0
- package/dist/vendor/error.cjs +18 -0
- package/dist/vendor/helpers/beta/environments.cjs +30 -0
- package/dist/vendor/helpers/beta/json-schema.cjs +56 -0
- package/dist/vendor/helpers/beta/mcp.cjs +418 -0
- package/dist/vendor/helpers/beta/memory.cjs +17 -0
- package/dist/vendor/helpers/beta/standard-schema.cjs +57 -0
- package/dist/vendor/helpers/beta/zod.cjs +88 -0
- package/dist/vendor/helpers/index.cjs +7 -0
- package/dist/vendor/helpers/json-schema.cjs +36 -0
- package/dist/vendor/helpers/zod.cjs +77 -0
- package/dist/vendor/index.cjs +54 -0
- package/dist/vendor/internal/builtin-types.cjs +3 -0
- package/dist/vendor/internal/constants.cjs +12 -0
- package/dist/vendor/internal/decoders/jsonl.cjs +40 -0
- package/dist/vendor/internal/decoders/line.cjs +109 -0
- package/dist/vendor/internal/detect-platform.cjs +159 -0
- package/dist/vendor/internal/errors.cjs +42 -0
- package/dist/vendor/internal/file-store.cjs +535 -0
- package/dist/vendor/internal/headers.cjs +119 -0
- package/dist/vendor/internal/message-stream-utils.cjs +33 -0
- package/dist/vendor/internal/node.browser.cjs +21 -0
- package/dist/vendor/internal/node.cjs +55 -0
- package/dist/vendor/internal/parse.cjs +65 -0
- package/dist/vendor/internal/qs/formats.cjs +12 -0
- package/dist/vendor/internal/qs/index.cjs +13 -0
- package/dist/vendor/internal/qs/stringify.cjs +276 -0
- package/dist/vendor/internal/qs/types.cjs +2 -0
- package/dist/vendor/internal/qs/utils.cjs +229 -0
- package/dist/vendor/internal/request-options.cjs +31 -0
- package/dist/vendor/internal/request-signal.cjs +49 -0
- package/dist/vendor/internal/shim-types.cjs +3 -0
- package/dist/vendor/internal/shims.cjs +91 -0
- package/dist/vendor/internal/stainless-helper-header.cjs +90 -0
- package/dist/vendor/internal/stream-utils.cjs +37 -0
- package/dist/vendor/internal/to-file.cjs +95 -0
- package/dist/vendor/internal/types.cjs +3 -0
- package/dist/vendor/internal/uploads.cjs +148 -0
- package/dist/vendor/internal/utils/abort.cjs +24 -0
- package/dist/vendor/internal/utils/async-queue.cjs +69 -0
- package/dist/vendor/internal/utils/backoff.cjs +43 -0
- package/dist/vendor/internal/utils/base64.cjs +37 -0
- package/dist/vendor/internal/utils/bytes.cjs +30 -0
- package/dist/vendor/internal/utils/env.cjs +21 -0
- package/dist/vendor/internal/utils/log.cjs +111 -0
- package/dist/vendor/internal/utils/path.cjs +78 -0
- package/dist/vendor/internal/utils/promise.cjs +17 -0
- package/dist/vendor/internal/utils/query.cjs +41 -0
- package/dist/vendor/internal/utils/sleep.cjs +27 -0
- package/dist/vendor/internal/utils/time.cjs +7 -0
- package/dist/vendor/internal/utils/uuid.cjs +18 -0
- package/dist/vendor/internal/utils/values.cjs +125 -0
- package/dist/vendor/internal/utils.cjs +24 -0
- package/dist/vendor/lib/BetaMessageStream.cjs +665 -0
- package/dist/vendor/lib/MessageStream.cjs +612 -0
- package/dist/vendor/lib/beta-parser.cjs +78 -0
- package/dist/vendor/lib/credentials/credential-chain.cjs +244 -0
- package/dist/vendor/lib/credentials/identity-token.cjs +71 -0
- package/dist/vendor/lib/credentials/oidc-federation.cjs +81 -0
- package/dist/vendor/lib/credentials/token-cache.cjs +113 -0
- package/dist/vendor/lib/credentials/types.cjs +267 -0
- package/dist/vendor/lib/credentials/user-oauth.cjs +129 -0
- package/dist/vendor/lib/credentials.cjs +7 -0
- package/dist/vendor/lib/environments/index.cjs +17 -0
- package/dist/vendor/lib/environments/poller.cjs +226 -0
- package/dist/vendor/lib/environments/worker.cjs +609 -0
- package/dist/vendor/lib/helper-client.cjs +57 -0
- package/dist/vendor/lib/middleware.cjs +779 -0
- package/dist/vendor/lib/parser.cjs +65 -0
- package/dist/vendor/lib/sessions/accumulate.cjs +44 -0
- package/dist/vendor/lib/standard-schema.cjs +44 -0
- package/dist/vendor/lib/tools/BetaRunnableTool.cjs +30 -0
- package/dist/vendor/lib/tools/BetaToolRunner.cjs +500 -0
- package/dist/vendor/lib/tools/CompactionControl.cjs +27 -0
- package/dist/vendor/lib/tools/SessionToolRunner.cjs +740 -0
- package/dist/vendor/lib/tools/ToolError.cjs +46 -0
- package/dist/vendor/lib/transform-json-schema.cjs +113 -0
- package/dist/vendor/pagination.cjs +18 -0
- package/dist/vendor/resource.cjs +18 -0
- package/dist/vendor/resources/beta/agents/agents.cjs +159 -0
- package/dist/vendor/resources/beta/agents/index.cjs +8 -0
- package/dist/vendor/resources/beta/agents/versions.cjs +35 -0
- package/dist/vendor/resources/beta/agents.cjs +18 -0
- package/dist/vendor/resources/beta/beta.cjs +105 -0
- package/dist/vendor/resources/beta/deployment-runs.cjs +54 -0
- package/dist/vendor/resources/beta/deployments.cjs +195 -0
- package/dist/vendor/resources/beta/dreams.cjs +115 -0
- package/dist/vendor/resources/beta/environments/environments.cjs +178 -0
- package/dist/vendor/resources/beta/environments/index.cjs +8 -0
- package/dist/vendor/resources/beta/environments/work.cjs +256 -0
- package/dist/vendor/resources/beta/environments.cjs +18 -0
- package/dist/vendor/resources/beta/files.cjs +123 -0
- package/dist/vendor/resources/beta/index.cjs +38 -0
- package/dist/vendor/resources/beta/memory-stores/index.cjs +10 -0
- package/dist/vendor/resources/beta/memory-stores/memories.cjs +129 -0
- package/dist/vendor/resources/beta/memory-stores/memory-stores.cjs +173 -0
- package/dist/vendor/resources/beta/memory-stores/memory-versions.cjs +80 -0
- package/dist/vendor/resources/beta/memory-stores.cjs +18 -0
- package/dist/vendor/resources/beta/messages/batches.cjs +206 -0
- package/dist/vendor/resources/beta/messages/index.cjs +11 -0
- package/dist/vendor/resources/beta/messages/messages.cjs +189 -0
- package/dist/vendor/resources/beta/messages.cjs +18 -0
- package/dist/vendor/resources/beta/models.cjs +59 -0
- package/dist/vendor/resources/beta/organization/api-keys.cjs +55 -0
- package/dist/vendor/resources/beta/organization/compliance-settings.cjs +51 -0
- package/dist/vendor/resources/beta/organization/external-keys.cjs +118 -0
- package/dist/vendor/resources/beta/organization/federation/federation.cjs +49 -0
- package/dist/vendor/resources/beta/organization/federation/index.cjs +10 -0
- package/dist/vendor/resources/beta/organization/federation/issuers.cjs +168 -0
- package/dist/vendor/resources/beta/organization/federation/rules/index.cjs +8 -0
- package/dist/vendor/resources/beta/organization/federation/rules/rules.cjs +226 -0
- package/dist/vendor/resources/beta/organization/federation/rules/workspaces.cjs +113 -0
- package/dist/vendor/resources/beta/organization/federation/rules.cjs +18 -0
- package/dist/vendor/resources/beta/organization/federation.cjs +18 -0
- package/dist/vendor/resources/beta/organization/index.cjs +24 -0
- package/dist/vendor/resources/beta/organization/invites.cjs +75 -0
- package/dist/vendor/resources/beta/organization/organization.cjs +90 -0
- package/dist/vendor/resources/beta/organization/rate-limits.cjs +30 -0
- package/dist/vendor/resources/beta/organization/service-accounts/index.cjs +8 -0
- package/dist/vendor/resources/beta/organization/service-accounts/service-accounts.cjs +202 -0
- package/dist/vendor/resources/beta/organization/service-accounts/workspaces.cjs +124 -0
- package/dist/vendor/resources/beta/organization/service-accounts.cjs +18 -0
- package/dist/vendor/resources/beta/organization/users.cjs +66 -0
- package/dist/vendor/resources/beta/organization/workspaces/index.cjs +12 -0
- package/dist/vendor/resources/beta/organization/workspaces/members.cjs +101 -0
- package/dist/vendor/resources/beta/organization/workspaces/rate-limits.cjs +33 -0
- package/dist/vendor/resources/beta/organization/workspaces/service-accounts.cjs +188 -0
- package/dist/vendor/resources/beta/organization/workspaces/workspaces.cjs +140 -0
- package/dist/vendor/resources/beta/organization/workspaces.cjs +18 -0
- package/dist/vendor/resources/beta/organization.cjs +18 -0
- package/dist/vendor/resources/beta/sessions/events.cjs +119 -0
- package/dist/vendor/resources/beta/sessions/index.cjs +12 -0
- package/dist/vendor/resources/beta/sessions/resources.cjs +131 -0
- package/dist/vendor/resources/beta/sessions/sessions.cjs +186 -0
- package/dist/vendor/resources/beta/sessions/threads/events.cjs +60 -0
- package/dist/vendor/resources/beta/sessions/threads/index.cjs +8 -0
- package/dist/vendor/resources/beta/sessions/threads/threads.cjs +116 -0
- package/dist/vendor/resources/beta/sessions/threads.cjs +18 -0
- package/dist/vendor/resources/beta/sessions.cjs +18 -0
- package/dist/vendor/resources/beta/skills/index.cjs +8 -0
- package/dist/vendor/resources/beta/skills/skills.cjs +132 -0
- package/dist/vendor/resources/beta/skills/versions.cjs +128 -0
- package/dist/vendor/resources/beta/skills.cjs +18 -0
- package/dist/vendor/resources/beta/tunnels/certificates.cjs +128 -0
- package/dist/vendor/resources/beta/tunnels/index.cjs +8 -0
- package/dist/vendor/resources/beta/tunnels/tunnels.cjs +209 -0
- package/dist/vendor/resources/beta/tunnels.cjs +18 -0
- package/dist/vendor/resources/beta/user-profiles.cjs +117 -0
- package/dist/vendor/resources/beta/vaults/credentials.cjs +176 -0
- package/dist/vendor/resources/beta/vaults/index.cjs +8 -0
- package/dist/vendor/resources/beta/vaults/vaults.cjs +177 -0
- package/dist/vendor/resources/beta/vaults.cjs +18 -0
- package/dist/vendor/resources/beta/webhooks.cjs +32 -0
- package/dist/vendor/resources/beta.cjs +18 -0
- package/dist/vendor/resources/completions.cjs +22 -0
- package/dist/vendor/resources/files.cjs +51 -0
- package/dist/vendor/resources/index.cjs +31 -0
- package/dist/vendor/resources/messages/batches.cjs +160 -0
- package/dist/vendor/resources/messages/index.cjs +8 -0
- package/dist/vendor/resources/messages/messages.cjs +159 -0
- package/dist/vendor/resources/messages.cjs +18 -0
- package/dist/vendor/resources/models.cjs +44 -0
- package/dist/vendor/resources/shared.cjs +3 -0
- package/dist/vendor/resources/skills/index.cjs +8 -0
- package/dist/vendor/resources/skills/skills.cjs +72 -0
- package/dist/vendor/resources/skills/versions.cjs +40 -0
- package/dist/vendor/resources/skills.cjs +18 -0
- package/dist/vendor/resources/top-level.cjs +3 -0
- package/dist/vendor/resources.cjs +17 -0
- package/dist/vendor/streaming.cjs +18 -0
- package/dist/vendor/tools/agent-toolset/fs-util.cjs +193 -0
- package/dist/vendor/tools/agent-toolset/memories.cjs +916 -0
- package/dist/vendor/tools/agent-toolset/node.browser.cjs +147 -0
- package/dist/vendor/tools/agent-toolset/node.cjs +939 -0
- package/dist/vendor/tools/agent-toolset/skills.cjs +325 -0
- package/dist/vendor/tools/agent-toolset/sync-interval.cjs +28 -0
- package/dist/vendor/tools/memory/node.browser.cjs +48 -0
- package/dist/vendor/tools/memory/node.cjs +364 -0
- package/dist/vendor/uploads.cjs +18 -0
- package/dist/vendor/version.cjs +4 -0
- package/package.json +12 -13
- package/sdk.cjs +0 -14271
- package/sdk.cjs.map +0 -149
- package/sdk.d.ts +0 -10866
- package/sdk.mjs +0 -68
- package/sdk.mjs.map +0 -149
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isFetchOriginError = isFetchOriginError;
|
|
4
|
+
exports.isRetryableError = isRetryableError;
|
|
5
|
+
exports.wrapFetchWithMiddleware = wrapFetchWithMiddleware;
|
|
6
|
+
exports.applyMiddleware = applyMiddleware;
|
|
7
|
+
const errors_1 = require("../internal/errors");
|
|
8
|
+
const parse_1 = require("../internal/parse");
|
|
9
|
+
const log_1 = require("../internal/utils/log");
|
|
10
|
+
const error_1 = require("./error");
|
|
11
|
+
const streaming_1 = require("./streaming");
|
|
12
|
+
/**
|
|
13
|
+
* Errors thrown by the underlying `fetch`, as opposed to by a middleware.
|
|
14
|
+
*
|
|
15
|
+
* Tracked so the client can apply its connection-error retry policy to
|
|
16
|
+
* transport failures while letting errors thrown by middleware propagate to
|
|
17
|
+
* the caller untouched.
|
|
18
|
+
*/
|
|
19
|
+
const fetchOriginErrors = new WeakSet();
|
|
20
|
+
/** Whether `err` was thrown by the underlying `fetch` rather than by a middleware. */
|
|
21
|
+
function isFetchOriginError(err) {
|
|
22
|
+
return typeof err === 'object' && err !== null && fetchOriginErrors.has(err);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Whether an error thrown by middleware should stay on the SDK's
|
|
26
|
+
* connection-error retry policy: fetch-origin, abort, `APIConnectionError`, or
|
|
27
|
+
* `RetryableError` — checked through the error's `cause` chain.
|
|
28
|
+
*/
|
|
29
|
+
function isRetryableError(err) {
|
|
30
|
+
const seen = new Set(); // guard against `cause` cycles
|
|
31
|
+
while (typeof err === 'object' && err !== null && !seen.has(err)) {
|
|
32
|
+
seen.add(err);
|
|
33
|
+
if (isFetchOriginError(err) ||
|
|
34
|
+
(0, errors_1.isAbortError)(err) ||
|
|
35
|
+
err instanceof error_1.APIConnectionError ||
|
|
36
|
+
err instanceof error_1.RetryableError) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
err = err.cause;
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Wraps `fetchFn` so each call runs through `middleware`, keeping the same
|
|
45
|
+
* call signature as `fetch` itself.
|
|
46
|
+
*
|
|
47
|
+
* With no middleware, calls are passed straight through to `fetchFn`.
|
|
48
|
+
* Otherwise the arguments are normalized into an {@link APIRequest} (headers
|
|
49
|
+
* coerced to a `Headers` instance, URL stringified) before entering the
|
|
50
|
+
* chain. The chain is composed per call, so mutations of a `middleware`
|
|
51
|
+
* array are picked up by later requests.
|
|
52
|
+
*
|
|
53
|
+
* `options` — the SDK request options behind this call, when there are any —
|
|
54
|
+
* is surfaced to middleware as `ctx.options` and drives `ctx.parse`.
|
|
55
|
+
*
|
|
56
|
+
* `client` supplies `ctx.logger` (the client's level-filtered logger);
|
|
57
|
+
* without it, `ctx.logger` falls back to the client defaults: `console`,
|
|
58
|
+
* filtered to `PUKU_LOG` or `'warn'`.
|
|
59
|
+
*/
|
|
60
|
+
function wrapFetchWithMiddleware(fetchFn, middleware, options, client) {
|
|
61
|
+
return async (url, init = {}) => {
|
|
62
|
+
if (middleware.length === 0) {
|
|
63
|
+
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
|
|
64
|
+
return fetchFn.call(undefined, url, init);
|
|
65
|
+
}
|
|
66
|
+
const headers = init.headers instanceof Headers ? init.headers : new Headers(init.headers);
|
|
67
|
+
const response = await applyMiddleware(fetchFn, middleware, options, client)({
|
|
68
|
+
...init,
|
|
69
|
+
headers,
|
|
70
|
+
url: typeof url === 'string' ? url
|
|
71
|
+
: url instanceof URL ? url.href
|
|
72
|
+
: url.url,
|
|
73
|
+
});
|
|
74
|
+
// Catch a footgun before the client tries to read the body itself and
|
|
75
|
+
// fails with a confusing low-level stream error.
|
|
76
|
+
if (response.bodyUsed || response.body?.locked) {
|
|
77
|
+
throw new error_1.PukuError('middleware consumed the response body; use response.clone() to inspect it, ' +
|
|
78
|
+
'or return new Response(body, response) to consume and replace it');
|
|
79
|
+
}
|
|
80
|
+
return response;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Creates the {@link MiddlewareContext} shared by every middleware in one chain.
|
|
85
|
+
*/
|
|
86
|
+
function createMiddlewareContext(options, client) {
|
|
87
|
+
// Keyed on the Response so each `next()` call's response (e.g. with custom
|
|
88
|
+
// retries, or a middleware swapping in a replacement) parses independently,
|
|
89
|
+
// while several middleware parsing the same response share a single read.
|
|
90
|
+
const cache = new WeakMap();
|
|
91
|
+
return {
|
|
92
|
+
options,
|
|
93
|
+
// Resolved per chain, so changes to the client's `logLevel`/`logger`
|
|
94
|
+
// apply to subsequent requests.
|
|
95
|
+
logger: client ? (0, log_1.loggerFor)(client) : (0, log_1.defaultLogger)(),
|
|
96
|
+
parse(response) {
|
|
97
|
+
// Streams are single-consumer, so caching one would hand later callers
|
|
98
|
+
// an already-consumed stream; every call gets a fresh clone-backed one.
|
|
99
|
+
if (options?.stream && response.ok) {
|
|
100
|
+
return parseMiddlewareResponse(response, options, client);
|
|
101
|
+
}
|
|
102
|
+
let parsed = cache.get(response);
|
|
103
|
+
if (!parsed) {
|
|
104
|
+
parsed = parseMiddlewareResponse(response, options, client);
|
|
105
|
+
cache.set(response, parsed);
|
|
106
|
+
}
|
|
107
|
+
return parsed;
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Mirrors the client's own response parsing (`defaultParseResponse` in
|
|
113
|
+
* `internal/parse.ts`), reading through a clone so the body stays available
|
|
114
|
+
* to the rest of the chain and the client itself.
|
|
115
|
+
*/
|
|
116
|
+
async function parseMiddlewareResponse(response, options, client) {
|
|
117
|
+
if (response.bodyUsed || response.body?.locked) {
|
|
118
|
+
throw new error_1.PukuError('cannot ctx.parse() a response whose body was already consumed; ' +
|
|
119
|
+
'call ctx.parse() instead of reading the body, or read via response.clone()');
|
|
120
|
+
}
|
|
121
|
+
// Error responses parse as JSON/text below — the SDK only stream-parses
|
|
122
|
+
// successful responses, and middleware typically wants the error body.
|
|
123
|
+
if (options?.stream && response.ok) {
|
|
124
|
+
// A fresh controller rather than the request's own: aborting (or
|
|
125
|
+
// `break`ing out of) the middleware's stream must not cancel the
|
|
126
|
+
// in-flight request the client is still reading.
|
|
127
|
+
return streaming_1.Stream.fromSSEResponse(response.clone(), new AbortController(), client);
|
|
128
|
+
}
|
|
129
|
+
// fetch refuses to read the body when the status code is 204.
|
|
130
|
+
if (response.status === 204) {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
if (options?.__binaryResponse) {
|
|
134
|
+
return response;
|
|
135
|
+
}
|
|
136
|
+
const contentType = response.headers.get('content-type');
|
|
137
|
+
const mediaType = contentType?.split(';')[0]?.trim();
|
|
138
|
+
const isJSON = mediaType?.includes('application/json') || mediaType?.endsWith('+json');
|
|
139
|
+
if (isJSON) {
|
|
140
|
+
if (response.headers.get('content-length') === '0') {
|
|
141
|
+
// if there is no content we can't do anything
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
return (0, parse_1.addResponseIDs)(await response.clone().json(), response);
|
|
145
|
+
}
|
|
146
|
+
return await response.clone().text();
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Composes `middleware` around `fetchFn` and returns the entry point of the chain.
|
|
150
|
+
*/
|
|
151
|
+
function applyMiddleware(fetchFn, middleware, options, client) {
|
|
152
|
+
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
|
|
153
|
+
let next = async ({ url, ...init }) => {
|
|
154
|
+
try {
|
|
155
|
+
return await fetchFn.call(undefined, url, init);
|
|
156
|
+
}
|
|
157
|
+
catch (err) {
|
|
158
|
+
// Brand the error as fetch-origin, normalizing with `castToError` first since a
|
|
159
|
+
// WeakSet can't hold primitives and the brand must be on the same object the
|
|
160
|
+
// client's own `castToError` will later pass through.
|
|
161
|
+
const error = (0, errors_1.castToError)(err);
|
|
162
|
+
fetchOriginErrors.add(error);
|
|
163
|
+
throw error;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
const ctx = createMiddlewareContext(options, client);
|
|
167
|
+
for (let i = middleware.length - 1; i >= 0; i--) {
|
|
168
|
+
const mw = middleware[i];
|
|
169
|
+
const nextInner = next;
|
|
170
|
+
next = async (request) => mw(request, nextInner, ctx);
|
|
171
|
+
}
|
|
172
|
+
return next;
|
|
173
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.BidirectionalPageCursor = exports.PageCursor = exports.TokenPage = exports.Page = exports.PagePromise = exports.AbstractPage = void 0;
|
|
5
|
+
const error_1 = require("./error");
|
|
6
|
+
const parse_1 = require("../internal/parse");
|
|
7
|
+
const api_promise_1 = require("./api-promise");
|
|
8
|
+
const values_1 = require("../internal/utils/values");
|
|
9
|
+
class AbstractPage {
|
|
10
|
+
#client;
|
|
11
|
+
options;
|
|
12
|
+
response;
|
|
13
|
+
body;
|
|
14
|
+
constructor(client, response, body, options) {
|
|
15
|
+
this.#client = client;
|
|
16
|
+
this.options = options;
|
|
17
|
+
this.response = response;
|
|
18
|
+
this.body = body;
|
|
19
|
+
}
|
|
20
|
+
hasNextPage() {
|
|
21
|
+
const items = this.getPaginatedItems();
|
|
22
|
+
if (!items.length)
|
|
23
|
+
return false;
|
|
24
|
+
return this.nextPageRequestOptions() != null;
|
|
25
|
+
}
|
|
26
|
+
async getNextPage() {
|
|
27
|
+
const nextOptions = this.nextPageRequestOptions();
|
|
28
|
+
if (!nextOptions) {
|
|
29
|
+
throw new error_1.PukuError('No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.');
|
|
30
|
+
}
|
|
31
|
+
return await this.#client.requestAPIList(this.constructor, nextOptions);
|
|
32
|
+
}
|
|
33
|
+
async *iterPages() {
|
|
34
|
+
let page = this;
|
|
35
|
+
yield page;
|
|
36
|
+
while (page.hasNextPage()) {
|
|
37
|
+
page = await page.getNextPage();
|
|
38
|
+
yield page;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async *[Symbol.asyncIterator]() {
|
|
42
|
+
for await (const page of this.iterPages()) {
|
|
43
|
+
for (const item of page.getPaginatedItems()) {
|
|
44
|
+
yield item;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.AbstractPage = AbstractPage;
|
|
50
|
+
/**
|
|
51
|
+
* This subclass of Promise will resolve to an instantiated Page once the request completes.
|
|
52
|
+
*
|
|
53
|
+
* It also implements AsyncIterable to allow auto-paginating iteration on an unawaited list call, eg:
|
|
54
|
+
*
|
|
55
|
+
* for await (const item of client.items.list()) {
|
|
56
|
+
* console.log(item)
|
|
57
|
+
* }
|
|
58
|
+
*/
|
|
59
|
+
class PagePromise extends api_promise_1.APIPromise {
|
|
60
|
+
constructor(client, request, Page) {
|
|
61
|
+
super(client, request, async (client, props) => new Page(client, props.response, await (0, parse_1.defaultParseResponse)(client, props), props.options));
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Allow auto-paginating iteration on an unawaited list call, eg:
|
|
65
|
+
*
|
|
66
|
+
* for await (const item of client.items.list()) {
|
|
67
|
+
* console.log(item)
|
|
68
|
+
* }
|
|
69
|
+
*/
|
|
70
|
+
async *[Symbol.asyncIterator]() {
|
|
71
|
+
const page = await this;
|
|
72
|
+
for await (const item of page) {
|
|
73
|
+
yield item;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.PagePromise = PagePromise;
|
|
78
|
+
class Page extends AbstractPage {
|
|
79
|
+
data;
|
|
80
|
+
has_more;
|
|
81
|
+
first_id;
|
|
82
|
+
last_id;
|
|
83
|
+
constructor(client, response, body, options) {
|
|
84
|
+
super(client, response, body, options);
|
|
85
|
+
this.data = body.data || [];
|
|
86
|
+
this.has_more = body.has_more || false;
|
|
87
|
+
this.first_id = body.first_id || null;
|
|
88
|
+
this.last_id = body.last_id || null;
|
|
89
|
+
}
|
|
90
|
+
getPaginatedItems() {
|
|
91
|
+
return this.data ?? [];
|
|
92
|
+
}
|
|
93
|
+
hasNextPage() {
|
|
94
|
+
if (this.has_more === false) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
return super.hasNextPage();
|
|
98
|
+
}
|
|
99
|
+
nextPageRequestOptions() {
|
|
100
|
+
if (this.options.query?.['before_id']) {
|
|
101
|
+
// in reverse
|
|
102
|
+
const first_id = this.first_id;
|
|
103
|
+
if (!first_id) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
...this.options,
|
|
108
|
+
query: {
|
|
109
|
+
...(0, values_1.maybeObj)(this.options.query),
|
|
110
|
+
before_id: first_id,
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
const cursor = this.last_id;
|
|
115
|
+
if (!cursor) {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
...this.options,
|
|
120
|
+
query: {
|
|
121
|
+
...(0, values_1.maybeObj)(this.options.query),
|
|
122
|
+
after_id: cursor,
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
exports.Page = Page;
|
|
128
|
+
class TokenPage extends AbstractPage {
|
|
129
|
+
data;
|
|
130
|
+
has_more;
|
|
131
|
+
next_page;
|
|
132
|
+
constructor(client, response, body, options) {
|
|
133
|
+
super(client, response, body, options);
|
|
134
|
+
this.data = body.data || [];
|
|
135
|
+
this.has_more = body.has_more || false;
|
|
136
|
+
this.next_page = body.next_page || null;
|
|
137
|
+
}
|
|
138
|
+
getPaginatedItems() {
|
|
139
|
+
return this.data ?? [];
|
|
140
|
+
}
|
|
141
|
+
hasNextPage() {
|
|
142
|
+
if (this.has_more === false) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
return super.hasNextPage();
|
|
146
|
+
}
|
|
147
|
+
nextPageRequestOptions() {
|
|
148
|
+
const cursor = this.next_page;
|
|
149
|
+
if (!cursor) {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
...this.options,
|
|
154
|
+
query: {
|
|
155
|
+
...(0, values_1.maybeObj)(this.options.query),
|
|
156
|
+
page_token: cursor,
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
exports.TokenPage = TokenPage;
|
|
162
|
+
class PageCursor extends AbstractPage {
|
|
163
|
+
data;
|
|
164
|
+
next_page;
|
|
165
|
+
constructor(client, response, body, options) {
|
|
166
|
+
super(client, response, body, options);
|
|
167
|
+
this.data = body.data || [];
|
|
168
|
+
this.next_page = body.next_page || null;
|
|
169
|
+
}
|
|
170
|
+
getPaginatedItems() {
|
|
171
|
+
return this.data ?? [];
|
|
172
|
+
}
|
|
173
|
+
nextPageRequestOptions() {
|
|
174
|
+
const cursor = this.next_page;
|
|
175
|
+
if (!cursor) {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
...this.options,
|
|
180
|
+
query: {
|
|
181
|
+
...(0, values_1.maybeObj)(this.options.query),
|
|
182
|
+
page: cursor,
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
exports.PageCursor = PageCursor;
|
|
188
|
+
class BidirectionalPageCursor extends AbstractPage {
|
|
189
|
+
data;
|
|
190
|
+
next_page;
|
|
191
|
+
prev_page;
|
|
192
|
+
constructor(client, response, body, options) {
|
|
193
|
+
super(client, response, body, options);
|
|
194
|
+
this.data = body.data || [];
|
|
195
|
+
this.next_page = body.next_page || null;
|
|
196
|
+
this.prev_page = body.prev_page || null;
|
|
197
|
+
}
|
|
198
|
+
getPaginatedItems() {
|
|
199
|
+
return this.data ?? [];
|
|
200
|
+
}
|
|
201
|
+
nextPageRequestOptions() {
|
|
202
|
+
const cursor = this.next_page;
|
|
203
|
+
if (!cursor) {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
...this.options,
|
|
208
|
+
query: {
|
|
209
|
+
...(0, values_1.maybeObj)(this.options.query),
|
|
210
|
+
page: cursor,
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
exports.BidirectionalPageCursor = BidirectionalPageCursor;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.APIResource = void 0;
|
|
5
|
+
class APIResource {
|
|
6
|
+
_client;
|
|
7
|
+
constructor(client) {
|
|
8
|
+
this._client = client;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.APIResource = APIResource;
|