@superblocksteam/sdk-api 0.0.12 → 2.0.0-SNAPSHOT.1d540461e97d05b4
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/dist/errors.d.ts +28 -3
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +187 -8
- package/dist/errors.js.map +1 -1
- package/dist/errors.test.d.ts +2 -0
- package/dist/errors.test.d.ts.map +1 -0
- package/dist/errors.test.js +189 -0
- package/dist/errors.test.js.map +1 -0
- package/dist/integrations/anthropic/types.d.ts +1 -1
- package/dist/integrations/base/decode-worker-binary-response.d.ts.map +1 -1
- package/dist/integrations/base/decode-worker-binary-response.js +2 -5
- package/dist/integrations/base/decode-worker-binary-response.js.map +1 -1
- package/dist/integrations/base/index.d.ts +2 -1
- package/dist/integrations/base/index.d.ts.map +1 -1
- package/dist/integrations/base/index.js +1 -0
- package/dist/integrations/base/index.js.map +1 -1
- package/dist/integrations/base/request-body.d.ts +29 -0
- package/dist/integrations/base/request-body.d.ts.map +1 -0
- package/dist/integrations/base/request-body.js +217 -0
- package/dist/integrations/base/request-body.js.map +1 -0
- package/dist/integrations/base/request-body.test.d.ts +2 -0
- package/dist/integrations/base/request-body.test.d.ts.map +1 -0
- package/dist/integrations/base/request-body.test.js +448 -0
- package/dist/integrations/base/request-body.test.js.map +1 -0
- package/dist/integrations/base/rest-api-client-base.d.ts.map +1 -1
- package/dist/integrations/base/rest-api-client-base.js +15 -14
- package/dist/integrations/base/rest-api-client-base.js.map +1 -1
- package/dist/integrations/base/rest-api-integration-client.d.ts.map +1 -1
- package/dist/integrations/base/rest-api-integration-client.js +2 -5
- package/dist/integrations/base/rest-api-integration-client.js.map +1 -1
- package/dist/integrations/base/types.d.ts +32 -5
- package/dist/integrations/base/types.d.ts.map +1 -1
- package/dist/integrations/base/types.js +7 -0
- package/dist/integrations/base/types.js.map +1 -1
- package/dist/integrations/cosmosdb/client.d.ts.map +1 -1
- package/dist/integrations/cosmosdb/client.js +2 -5
- package/dist/integrations/cosmosdb/client.js.map +1 -1
- package/dist/integrations/documentation-resolver.test.js +53 -1
- package/dist/integrations/documentation-resolver.test.js.map +1 -1
- package/dist/integrations/documentation.test.js +10 -0
- package/dist/integrations/documentation.test.js.map +1 -1
- package/dist/integrations/dynamodb/client.d.ts.map +1 -1
- package/dist/integrations/dynamodb/client.js +2 -5
- package/dist/integrations/dynamodb/client.js.map +1 -1
- package/dist/integrations/gcs/client.d.ts.map +1 -1
- package/dist/integrations/gcs/client.js +2 -5
- package/dist/integrations/gcs/client.js.map +1 -1
- package/dist/integrations/gsheets/client.d.ts.map +1 -1
- package/dist/integrations/gsheets/client.js +2 -5
- package/dist/integrations/gsheets/client.js.map +1 -1
- package/dist/integrations/mongodb/client.d.ts.map +1 -1
- package/dist/integrations/mongodb/client.js +2 -5
- package/dist/integrations/mongodb/client.js.map +1 -1
- package/dist/integrations/restapiintegration/client.body-types.test.d.ts +2 -0
- package/dist/integrations/restapiintegration/client.body-types.test.d.ts.map +1 -0
- package/dist/integrations/restapiintegration/client.body-types.test.js +227 -0
- package/dist/integrations/restapiintegration/client.body-types.test.js.map +1 -0
- package/dist/integrations/restapiintegration/client.test.js +16 -0
- package/dist/integrations/restapiintegration/client.test.js.map +1 -1
- package/dist/integrations/s3/client.d.ts.map +1 -1
- package/dist/integrations/s3/client.js +2 -5
- package/dist/integrations/s3/client.js.map +1 -1
- package/dist/integrations/salesforce/client.d.ts.map +1 -1
- package/dist/integrations/salesforce/client.js +2 -5
- package/dist/integrations/salesforce/client.js.map +1 -1
- package/dist/integrations/slack/client.d.ts.map +1 -1
- package/dist/integrations/slack/client.js +4 -13
- package/dist/integrations/slack/client.js.map +1 -1
- package/dist/integrations/slack/client.test.js +20 -0
- package/dist/integrations/slack/client.test.js.map +1 -1
- package/dist/integrations/utils.d.ts +8 -3
- package/dist/integrations/utils.d.ts.map +1 -1
- package/dist/integrations/utils.js +42 -1
- package/dist/integrations/utils.js.map +1 -1
- package/package.json +6 -3
- package/src/errors.test.ts +257 -0
- package/src/errors.ts +214 -10
- package/src/integrations/anthropic/README.md +12 -59
- package/src/integrations/anthropic/types.ts +1 -1
- package/src/integrations/base/decode-worker-binary-response.ts +5 -7
- package/src/integrations/base/index.ts +2 -0
- package/src/integrations/base/request-body.test.ts +535 -0
- package/src/integrations/base/request-body.ts +368 -0
- package/src/integrations/base/rest-api-client-base.ts +24 -19
- package/src/integrations/base/rest-api-integration-client.ts +8 -7
- package/src/integrations/base/types.ts +41 -5
- package/src/integrations/cosmosdb/client.ts +8 -7
- package/src/integrations/documentation-resolver.test.ts +65 -1
- package/src/integrations/documentation.test.ts +12 -0
- package/src/integrations/dynamodb/client.ts +8 -7
- package/src/integrations/gcs/client.ts +8 -7
- package/src/integrations/gsheets/client.ts +8 -7
- package/src/integrations/jira/README.md +0 -26
- package/src/integrations/jira/docs.manifest.json +10 -1
- package/src/integrations/jira/overlays/attachments-unsupported.md +3 -0
- package/src/integrations/jira/overlays/attachments.md +30 -0
- package/src/integrations/mongodb/client.ts +8 -7
- package/src/integrations/restapiintegration/client.body-types.test.ts +323 -0
- package/src/integrations/restapiintegration/client.test.ts +23 -0
- package/src/integrations/restapiintegration/docs.manifest.json +8 -0
- package/src/integrations/restapiintegration/overlays/request-body-types-unsupported.md +7 -0
- package/src/integrations/restapiintegration/overlays/request-body-types.md +115 -0
- package/src/integrations/s3/client.ts +8 -7
- package/src/integrations/salesforce/client.ts +9 -7
- package/src/integrations/slack/client.test.ts +30 -0
- package/src/integrations/slack/client.ts +13 -19
- package/src/integrations/utils.ts +51 -1
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
import type { PartialMessage } from "@bufbuild/protobuf";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
import type { Property } from "@superblocksteam/types/dist/src/common/v1/plugin_pb";
|
|
5
|
+
|
|
6
|
+
import { isBinaryData } from "../../errors.js";
|
|
7
|
+
import { describeOptionValue, validationError } from "../utils.js";
|
|
8
|
+
import { REST_API_BODY_TYPES } from "./types.js";
|
|
9
|
+
|
|
10
|
+
type BinaryBody = ArrayBuffer | ArrayBufferView;
|
|
11
|
+
|
|
12
|
+
type RequestBodyWireType =
|
|
13
|
+
| "jsonBody"
|
|
14
|
+
| "rawBody"
|
|
15
|
+
| "formDataBase64"
|
|
16
|
+
| "binaryBody";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Fields safe to spread directly onto the proto `RestApiRequest`. Never
|
|
20
|
+
* carries `contentType`: that is a header directive, not a wire field, and
|
|
21
|
+
* lives on `EncodedRequestBody` instead so a caller cannot spread it onto
|
|
22
|
+
* the wire message by mistake.
|
|
23
|
+
*/
|
|
24
|
+
export type RequestBodyWireFields = {
|
|
25
|
+
readonly body?: string;
|
|
26
|
+
readonly formData?: PartialMessage<Property>[];
|
|
27
|
+
readonly bodyType?: RequestBodyWireType;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The encoder's full result: wire fields plus an optional Content-Type the
|
|
32
|
+
* caller must push onto request headers itself.
|
|
33
|
+
*/
|
|
34
|
+
export type EncodedRequestBody = {
|
|
35
|
+
readonly wire: RequestBodyWireFields;
|
|
36
|
+
readonly contentType?: string;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const requestBodyTypeSchema = z.enum(REST_API_BODY_TYPES);
|
|
40
|
+
|
|
41
|
+
export function encodeRequestBody(input: {
|
|
42
|
+
body: unknown;
|
|
43
|
+
bodyType?: unknown;
|
|
44
|
+
headers?: Record<string, string>;
|
|
45
|
+
}): EncodedRequestBody {
|
|
46
|
+
const bodyTypeResult = requestBodyTypeSchema.safeParse(
|
|
47
|
+
input.bodyType === undefined ? "json" : input.bodyType,
|
|
48
|
+
);
|
|
49
|
+
if (!bodyTypeResult.success) {
|
|
50
|
+
throw validationError(
|
|
51
|
+
`Unsupported bodyType ${describeOptionValue(input.bodyType)} - expected one of: ${REST_API_BODY_TYPES.join(", ")}`,
|
|
52
|
+
["bodyType"],
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const bodyType = bodyTypeResult.data;
|
|
57
|
+
if (input.body === undefined) {
|
|
58
|
+
if (bodyType === "json") return { wire: {} };
|
|
59
|
+
throw validationError(`bodyType "${bodyType}" requires a request body`, [
|
|
60
|
+
"body",
|
|
61
|
+
]);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
switch (bodyType) {
|
|
65
|
+
case "json":
|
|
66
|
+
return { wire: encodeJsonBody(input.body) };
|
|
67
|
+
case "raw":
|
|
68
|
+
return { wire: encodeRawBody(input.body) };
|
|
69
|
+
case "formUrlencoded":
|
|
70
|
+
return encodeFormUrlencodedBody(input.body, input.headers);
|
|
71
|
+
case "multipartForm":
|
|
72
|
+
return { wire: encodeMultipartFormBody(input.body, input.headers) };
|
|
73
|
+
case "binary":
|
|
74
|
+
return encodeBinaryBody(input.body, input.headers);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// JSON.stringify's own error text is unbounded: a circular-structure message
|
|
79
|
+
// embeds the caller's full property path and constructor names, and a
|
|
80
|
+
// throwing toJSON()/getter contributes an arbitrary caller-authored string.
|
|
81
|
+
// Every other message in this module goes through describeOptionValue's
|
|
82
|
+
// bounded conventions; classify the failure instead of echoing its text.
|
|
83
|
+
function describeJsonSerializationFailure(error: unknown): string {
|
|
84
|
+
if (
|
|
85
|
+
error instanceof TypeError &&
|
|
86
|
+
/circular (structure|reference)/i.test(error.message)
|
|
87
|
+
) {
|
|
88
|
+
return "the body contains a circular reference";
|
|
89
|
+
}
|
|
90
|
+
if (error instanceof TypeError && /BigInt/i.test(error.message)) {
|
|
91
|
+
return "the body contains a BigInt value";
|
|
92
|
+
}
|
|
93
|
+
return "a nested value's toJSON, toString, or getter threw while serializing the body";
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function encodeJsonBody(body: unknown): RequestBodyWireFields {
|
|
97
|
+
if (isBinaryData(body)) {
|
|
98
|
+
throw validationError('Binary request bodies require bodyType: "binary"', [
|
|
99
|
+
"body",
|
|
100
|
+
]);
|
|
101
|
+
}
|
|
102
|
+
let serialized: string | undefined;
|
|
103
|
+
try {
|
|
104
|
+
serialized = JSON.stringify(body);
|
|
105
|
+
} catch (error) {
|
|
106
|
+
throw validationError(
|
|
107
|
+
`bodyType "json" request body is not JSON-serializable: ${describeJsonSerializationFailure(error)}`,
|
|
108
|
+
["body"],
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
if (serialized === undefined) {
|
|
112
|
+
throw validationError(
|
|
113
|
+
`bodyType "json" cannot serialize a ${typeof body} request body`,
|
|
114
|
+
["body"],
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
return { body: serialized, bodyType: "jsonBody" };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function encodeRawBody(body: unknown): RequestBodyWireFields {
|
|
121
|
+
if (typeof body !== "string" || body.length === 0) {
|
|
122
|
+
throw validationError(
|
|
123
|
+
'bodyType "raw" requires a non-empty string request body',
|
|
124
|
+
["body"],
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
return { body, bodyType: "rawBody" };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// A field's value may be a scalar or an array; either way, undefined items
|
|
131
|
+
// (an explicitly-undefined optional field, or an undefined array element)
|
|
132
|
+
// are omitted rather than encoded, matching JSON.stringify's default-body
|
|
133
|
+
// semantics. Shared by encodeFormUrlencodedBody and encodeMultipartFormBody.
|
|
134
|
+
function definedArrayValues(value: unknown): unknown[] {
|
|
135
|
+
return (Array.isArray(value) ? value : [value]).filter(
|
|
136
|
+
(item) => item !== undefined,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function encodeFormUrlencodedBody(
|
|
141
|
+
body: unknown,
|
|
142
|
+
headers: Record<string, string> | undefined,
|
|
143
|
+
): EncodedRequestBody {
|
|
144
|
+
if (!isPlainRecord(body)) {
|
|
145
|
+
throw validationError(
|
|
146
|
+
'bodyType "formUrlencoded" requires a plain object request body',
|
|
147
|
+
["body"],
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
const pairs: string[] = [];
|
|
151
|
+
for (const [key, value] of Object.entries(body)) {
|
|
152
|
+
if (value === undefined) continue;
|
|
153
|
+
if (key.length === 0) {
|
|
154
|
+
throw validationError(
|
|
155
|
+
"formUrlencoded body keys must be non-empty strings",
|
|
156
|
+
["body", key],
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
const encodedKey = formUrlencode(key, key);
|
|
160
|
+
for (const item of definedArrayValues(value)) {
|
|
161
|
+
pairs.push(
|
|
162
|
+
`${encodedKey}=${formUrlencode(formUrlencodedPrimitiveToString(key, item), key)}`,
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (pairs.length === 0) {
|
|
167
|
+
throw validationError(
|
|
168
|
+
"formUrlencoded body must encode at least one field",
|
|
169
|
+
["body"],
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
wire: { body: pairs.join("&"), bodyType: "rawBody" },
|
|
174
|
+
contentType: contentTypeIfMissing(
|
|
175
|
+
headers,
|
|
176
|
+
"application/x-www-form-urlencoded",
|
|
177
|
+
),
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// The WASM sdk-api sandbox has no URLSearchParams global, so encode by hand.
|
|
182
|
+
// encodeURIComponent's unreserved set leaves `! ' ( ) ~` unescaped, but the
|
|
183
|
+
// WHATWG application/x-www-form-urlencoded safe set (what URLSearchParams
|
|
184
|
+
// produces) escapes those five; only alphanumerics, `* - . _`, and space
|
|
185
|
+
// (as `+`) stay literal.
|
|
186
|
+
function formUrlencode(value: string, key: string): string {
|
|
187
|
+
try {
|
|
188
|
+
return encodeURIComponent(value)
|
|
189
|
+
.replace(/%20/g, "+")
|
|
190
|
+
.replace(
|
|
191
|
+
/[!'()~]/g,
|
|
192
|
+
(char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`,
|
|
193
|
+
);
|
|
194
|
+
} catch {
|
|
195
|
+
throw validationError(
|
|
196
|
+
`formUrlencoded field ${describeOptionValue(key)} contains a lone surrogate and cannot be percent-encoded`,
|
|
197
|
+
["body", key],
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function formUrlencodedPrimitiveToString(key: string, value: unknown): string {
|
|
203
|
+
if (typeof value === "string" || typeof value === "boolean")
|
|
204
|
+
return String(value);
|
|
205
|
+
if (typeof value === "number" && Number.isFinite(value)) return String(value);
|
|
206
|
+
if (typeof value === "object" && value !== null) {
|
|
207
|
+
throw validationError(
|
|
208
|
+
`formUrlencoded field ${describeOptionValue(key)} is nested; flatten it using bracket notation such as "metadata[order_id]"`,
|
|
209
|
+
["body", key],
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
throw validationError(
|
|
213
|
+
`formUrlencoded field ${describeOptionValue(key)} must be a string, finite number, boolean, or array of those values`,
|
|
214
|
+
["body", key],
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function encodeBinaryBody(
|
|
219
|
+
body: unknown,
|
|
220
|
+
headers: Record<string, string> | undefined,
|
|
221
|
+
): EncodedRequestBody {
|
|
222
|
+
if (!isBinaryData(body)) {
|
|
223
|
+
throw validationError(
|
|
224
|
+
'bodyType "binary" requires an ArrayBuffer or ArrayBufferView request body',
|
|
225
|
+
["body"],
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
if (body.byteLength === 0) {
|
|
229
|
+
throw validationError("binary request bodies must be non-empty", ["body"]);
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
wire: { body: toBuffer(body).toString("base64"), bodyType: "binaryBody" },
|
|
233
|
+
contentType: contentTypeIfMissing(headers, "application/octet-stream"),
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function encodeMultipartFormBody(
|
|
238
|
+
body: unknown,
|
|
239
|
+
headers: Record<string, string> | undefined,
|
|
240
|
+
): RequestBodyWireFields {
|
|
241
|
+
if (hasHeader(headers, "content-type")) {
|
|
242
|
+
throw validationError(
|
|
243
|
+
"multipartForm bodies cannot provide Content-Type because the multipart boundary is generated at execution time",
|
|
244
|
+
["headers", "Content-Type"],
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
if (!isPlainRecord(body)) {
|
|
248
|
+
throw validationError(
|
|
249
|
+
'bodyType "multipartForm" requires a plain object request body',
|
|
250
|
+
["body"],
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
const formData = Object.entries(body).flatMap(([key, value]) =>
|
|
254
|
+
definedArrayValues(value).map((item) => parseMultipartPart(key, item)),
|
|
255
|
+
);
|
|
256
|
+
if (formData.length === 0) {
|
|
257
|
+
throw validationError("multipartForm body must encode at least one field", [
|
|
258
|
+
"body",
|
|
259
|
+
]);
|
|
260
|
+
}
|
|
261
|
+
return { bodyType: "formDataBase64", formData };
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function parseMultipartPart(
|
|
265
|
+
key: string,
|
|
266
|
+
value: unknown,
|
|
267
|
+
): PartialMessage<Property> {
|
|
268
|
+
if (key.length === 0)
|
|
269
|
+
throw validationError("multipartForm body keys must be non-empty strings", [
|
|
270
|
+
"body",
|
|
271
|
+
key,
|
|
272
|
+
]);
|
|
273
|
+
if (
|
|
274
|
+
typeof value === "string" ||
|
|
275
|
+
typeof value === "boolean" ||
|
|
276
|
+
(typeof value === "number" && Number.isFinite(value))
|
|
277
|
+
) {
|
|
278
|
+
return { key, value: String(value) };
|
|
279
|
+
}
|
|
280
|
+
if (isBinaryData(value)) {
|
|
281
|
+
throw validationError(
|
|
282
|
+
`multipartForm binary field ${describeOptionValue(key)} must be wrapped as { value, filename }`,
|
|
283
|
+
["body", key],
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
if (!isPlainRecord(value)) {
|
|
287
|
+
throw validationError(
|
|
288
|
+
`multipartForm field ${describeOptionValue(key)} must be a primitive value or { value, filename } file part`,
|
|
289
|
+
["body", key],
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
const keys = Object.keys(value).filter(
|
|
293
|
+
(partKey) => value[partKey] !== undefined,
|
|
294
|
+
);
|
|
295
|
+
const missingKeys = ["value", "filename"].filter(
|
|
296
|
+
(requiredKey) => !keys.includes(requiredKey),
|
|
297
|
+
);
|
|
298
|
+
if (missingKeys.length > 0) {
|
|
299
|
+
throw validationError(
|
|
300
|
+
`multipartForm file part ${describeOptionValue(key)} is missing required ${pluralize("key", missingKeys)}: ${missingKeys.join(", ")}`,
|
|
301
|
+
["body", key],
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
const unexpectedKeys = keys.filter(
|
|
305
|
+
(partKey) => partKey !== "value" && partKey !== "filename",
|
|
306
|
+
);
|
|
307
|
+
if (unexpectedKeys.length > 0) {
|
|
308
|
+
const shown = unexpectedKeys.slice(0, 5).map(describeOptionValue);
|
|
309
|
+
const suffix = unexpectedKeys.length > 5 ? ", ..." : "";
|
|
310
|
+
throw validationError(
|
|
311
|
+
`multipartForm file part ${describeOptionValue(key)} has unexpected ${pluralize("key", unexpectedKeys)}: ${shown.join(", ")}${suffix}`,
|
|
312
|
+
["body", key],
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
if (typeof value.filename !== "string" || value.filename.length === 0) {
|
|
316
|
+
throw validationError(
|
|
317
|
+
`multipartForm file part ${describeOptionValue(key)} requires a non-empty filename`,
|
|
318
|
+
["body", key, "filename"],
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
if (typeof value.value !== "string" && !isBinaryData(value.value)) {
|
|
322
|
+
throw validationError(
|
|
323
|
+
`multipartForm file part ${describeOptionValue(key)} value must be a string or binary body`,
|
|
324
|
+
["body", key, "value"],
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
return {
|
|
328
|
+
key,
|
|
329
|
+
value: toBuffer(value.value).toString("base64"),
|
|
330
|
+
file: { filename: value.filename },
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function isPlainRecord(value: unknown): value is Record<string, unknown> {
|
|
335
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
336
|
+
return false;
|
|
337
|
+
const prototype = Object.getPrototypeOf(value);
|
|
338
|
+
return prototype === Object.prototype || prototype === null;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function toBuffer(value: string | BinaryBody): Buffer {
|
|
342
|
+
if (typeof value === "string") return Buffer.from(value, "utf8");
|
|
343
|
+
if (value instanceof ArrayBuffer) return Buffer.from(value);
|
|
344
|
+
return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function contentTypeIfMissing(
|
|
348
|
+
headers: Record<string, string> | undefined,
|
|
349
|
+
value: string,
|
|
350
|
+
): string | undefined {
|
|
351
|
+
return hasHeader(headers, "content-type") ? undefined : value;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function hasHeader(
|
|
355
|
+
headers: Record<string, string> | undefined,
|
|
356
|
+
headerName: string,
|
|
357
|
+
): boolean {
|
|
358
|
+
return (
|
|
359
|
+
!!headers &&
|
|
360
|
+
Object.keys(headers).some(
|
|
361
|
+
(key) => key.toLowerCase() === headerName.toLowerCase(),
|
|
362
|
+
)
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function pluralize(word: string, values: readonly unknown[]): string {
|
|
367
|
+
return values.length === 1 ? word : `${word}s`;
|
|
368
|
+
}
|
|
@@ -4,10 +4,15 @@ import { z } from "zod";
|
|
|
4
4
|
import type { Property } from "@superblocksteam/types/dist/src/common/v1/plugin_pb";
|
|
5
5
|
import type { Plugin as RestApiIntegrationPlugin } from "@superblocksteam/types/dist/src/plugins/restapiintegration/v1/plugin_pb";
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
RestApiValidationError,
|
|
9
|
+
restApiValidationErrorFromZodError,
|
|
10
|
+
} from "../../errors.js";
|
|
8
11
|
import type { QueryExecutor, TraceMetadata } from "../registry.js";
|
|
9
12
|
import type { IntegrationConfig, IntegrationClientImpl } from "../types.js";
|
|
13
|
+
import { describeOptionValue, validationError } from "../utils.js";
|
|
10
14
|
import { decodeWorkerBinaryResponse } from "./decode-worker-binary-response.js";
|
|
15
|
+
import { encodeRequestBody } from "./request-body.js";
|
|
11
16
|
import { REST_API_RESPONSE_TYPES } from "./types.js";
|
|
12
17
|
import type { ApiRequestOptions } from "./types.js";
|
|
13
18
|
|
|
@@ -45,12 +50,10 @@ export abstract class RestApiClientBase implements IntegrationClientImpl {
|
|
|
45
50
|
if (options.body !== undefined && bodySchema) {
|
|
46
51
|
const bodyParseResult = bodySchema.safeParse(options.body);
|
|
47
52
|
if (!bodyParseResult.success) {
|
|
48
|
-
throw
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
data: options.body,
|
|
53
|
-
},
|
|
53
|
+
throw restApiValidationErrorFromZodError(
|
|
54
|
+
"Request body validation failed",
|
|
55
|
+
bodyParseResult.error,
|
|
56
|
+
options.body,
|
|
54
57
|
);
|
|
55
58
|
}
|
|
56
59
|
}
|
|
@@ -73,16 +76,22 @@ export abstract class RestApiClientBase implements IntegrationClientImpl {
|
|
|
73
76
|
.enum(REST_API_RESPONSE_TYPES)
|
|
74
77
|
.safeParse(options.responseType ?? "json");
|
|
75
78
|
if (!responseTypeResult.success) {
|
|
76
|
-
throw
|
|
77
|
-
`Unsupported responseType ${
|
|
78
|
-
|
|
79
|
-
zodError: responseTypeResult.error,
|
|
80
|
-
data: options.responseType,
|
|
81
|
-
},
|
|
79
|
+
throw validationError(
|
|
80
|
+
`Unsupported responseType ${describeOptionValue(options.responseType)} - expected one of: ${REST_API_RESPONSE_TYPES.join(", ")}`,
|
|
81
|
+
["responseType"],
|
|
82
82
|
);
|
|
83
83
|
}
|
|
84
84
|
const responseType = responseTypeResult.data;
|
|
85
85
|
|
|
86
|
+
const { wire: bodyWire, contentType } = encodeRequestBody({
|
|
87
|
+
body: options.body,
|
|
88
|
+
bodyType: options.bodyType,
|
|
89
|
+
headers: options.headers,
|
|
90
|
+
});
|
|
91
|
+
if (contentType !== undefined) {
|
|
92
|
+
headers.push(this.createParam("Content-Type", contentType));
|
|
93
|
+
}
|
|
94
|
+
|
|
86
95
|
const request: RestApiRequest = {
|
|
87
96
|
openApiAction: "genericHttpRequest",
|
|
88
97
|
httpMethod: options.method.toUpperCase(),
|
|
@@ -90,14 +99,10 @@ export abstract class RestApiClientBase implements IntegrationClientImpl {
|
|
|
90
99
|
headers,
|
|
91
100
|
params,
|
|
92
101
|
responseType,
|
|
102
|
+
...bodyWire,
|
|
93
103
|
};
|
|
94
104
|
|
|
95
|
-
|
|
96
|
-
request.body = JSON.stringify(options.body);
|
|
97
|
-
request.bodyType = "jsonBody";
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const result = await this.executeQuery({ ...request }, undefined, metadata);
|
|
105
|
+
const result = await this.executeQuery(request, undefined, metadata);
|
|
101
106
|
|
|
102
107
|
if (result === null || result === undefined) {
|
|
103
108
|
throw new RestApiValidationError(
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
RestApiValidationError,
|
|
5
|
+
restApiValidationErrorFromZodError,
|
|
6
|
+
} from "../../errors.js";
|
|
4
7
|
import type { TraceMetadata } from "../registry.js";
|
|
5
8
|
import { RestApiClientBase } from "./rest-api-client-base.js";
|
|
6
9
|
import type {
|
|
@@ -71,12 +74,10 @@ export abstract class RestApiIntegrationClient
|
|
|
71
74
|
const responseParseResult = schema.response.safeParse(result);
|
|
72
75
|
|
|
73
76
|
if (!responseParseResult.success) {
|
|
74
|
-
throw
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
zodError: responseParseResult.error,
|
|
79
|
-
},
|
|
77
|
+
throw restApiValidationErrorFromZodError(
|
|
78
|
+
"Response validation failed",
|
|
79
|
+
responseParseResult.error,
|
|
80
|
+
result,
|
|
80
81
|
);
|
|
81
82
|
}
|
|
82
83
|
|
|
@@ -8,6 +8,16 @@ export const REST_API_RESPONSE_TYPES = ["binary", "json", "text"] as const;
|
|
|
8
8
|
|
|
9
9
|
export type RestApiResponseType = (typeof REST_API_RESPONSE_TYPES)[number];
|
|
10
10
|
|
|
11
|
+
export const REST_API_BODY_TYPES = [
|
|
12
|
+
"json",
|
|
13
|
+
"raw",
|
|
14
|
+
"formUrlencoded",
|
|
15
|
+
"multipartForm",
|
|
16
|
+
"binary",
|
|
17
|
+
] as const;
|
|
18
|
+
|
|
19
|
+
export type RestApiBodyType = (typeof REST_API_BODY_TYPES)[number];
|
|
20
|
+
|
|
11
21
|
export interface ApiRequestOptions<TBody = unknown> {
|
|
12
22
|
/**
|
|
13
23
|
* HTTP method for the request.
|
|
@@ -25,6 +35,18 @@ export interface ApiRequestOptions<TBody = unknown> {
|
|
|
25
35
|
*/
|
|
26
36
|
body?: TBody;
|
|
27
37
|
|
|
38
|
+
/**
|
|
39
|
+
* How the request body should be encoded. Defaults to `"json"`.
|
|
40
|
+
*
|
|
41
|
+
* Use `"raw"` for caller-encoded string bodies, `"formUrlencoded"` for
|
|
42
|
+
* `application/x-www-form-urlencoded` payloads, `"multipartForm"` for
|
|
43
|
+
* multipart form fields and file parts, and `"binary"` for byte payloads.
|
|
44
|
+
* Worker wire values such as `"jsonBody"`, `"rawBody"`, `"formData"`,
|
|
45
|
+
* `"fileForm"`, `"binaryBody"`, and `"formDataBase64"` are not public SDK
|
|
46
|
+
* `bodyType` values.
|
|
47
|
+
*/
|
|
48
|
+
bodyType?: RestApiBodyType;
|
|
49
|
+
|
|
28
50
|
/**
|
|
29
51
|
* Optional query parameters
|
|
30
52
|
*/
|
|
@@ -64,16 +86,20 @@ export interface ApiRequestSchema<TBody = unknown, TResponse = unknown> {
|
|
|
64
86
|
/**
|
|
65
87
|
* Interface for integration clients that support generic API requests.
|
|
66
88
|
*
|
|
67
|
-
* Providing a response schema gives type-safe, validated results. Omitting
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
89
|
+
* Providing a response schema gives type-safe, validated results. Omitting it
|
|
90
|
+
* returns the decoded response: `Uint8Array` for `responseType: "binary"`, or
|
|
91
|
+
* `unknown` for `responseType: "text"`. JSON still requires a schema. Object
|
|
92
|
+
* schemas only make sense for JSON-shaped results.
|
|
93
|
+
*
|
|
94
|
+
* Request bodies default to JSON. Set `bodyType` to one of `"json"`, `"raw"`,
|
|
95
|
+
* `"formUrlencoded"`, `"multipartForm"`, or `"binary"` when the endpoint
|
|
96
|
+
* expects a different request encoding.
|
|
71
97
|
*/
|
|
72
98
|
export interface SupportsApiRequest {
|
|
73
99
|
/**
|
|
74
100
|
* Execute a generic API request with type-safe validation.
|
|
75
101
|
*
|
|
76
|
-
* @param options - Request configuration including method, path, params, and
|
|
102
|
+
* @param options - Request configuration including method, path, params, body, and bodyType
|
|
77
103
|
* @param schema - Zod schemas for request body and response validation
|
|
78
104
|
* @param metadata - Optional trace metadata for observability (label, description)
|
|
79
105
|
* @returns Validated response data
|
|
@@ -101,6 +127,16 @@ export interface SupportsApiRequest {
|
|
|
101
127
|
* { label: 'slack.postMessage', description: 'Post a message to #alerts' }
|
|
102
128
|
* );
|
|
103
129
|
* // result is typed as { ts: string; channel: string }
|
|
130
|
+
*
|
|
131
|
+
* await ctx.integrations.stripe.apiRequest(
|
|
132
|
+
* {
|
|
133
|
+
* method: 'POST',
|
|
134
|
+
* path: '/v1/charges',
|
|
135
|
+
* bodyType: 'formUrlencoded',
|
|
136
|
+
* body: { amount: 2000, currency: 'usd' },
|
|
137
|
+
* },
|
|
138
|
+
* { response: StripeChargeSchema }
|
|
139
|
+
* );
|
|
104
140
|
* ```
|
|
105
141
|
*/
|
|
106
142
|
apiRequest<TBody, TResponse>(
|
|
@@ -11,7 +11,10 @@ import type { z } from "zod";
|
|
|
11
11
|
|
|
12
12
|
import type { Plugin as CosmosDBPlugin } from "@superblocksteam/types/dist/src/plugins/cosmosdb/v1/plugin_pb";
|
|
13
13
|
|
|
14
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
RestApiValidationError,
|
|
16
|
+
restApiValidationErrorFromZodError,
|
|
17
|
+
} from "../../errors.js";
|
|
15
18
|
import { IntegrationError } from "../../runtime/errors.js";
|
|
16
19
|
import type { QueryExecutor, TraceMetadata } from "../registry.js";
|
|
17
20
|
import type { IntegrationConfig, IntegrationClientImpl } from "../types.js";
|
|
@@ -80,12 +83,10 @@ export class CosmosDBClientImpl
|
|
|
80
83
|
const parseResult = schema.safeParse(result);
|
|
81
84
|
|
|
82
85
|
if (!parseResult.success) {
|
|
83
|
-
throw
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
data: result,
|
|
88
|
-
},
|
|
86
|
+
throw restApiValidationErrorFromZodError(
|
|
87
|
+
"Result validation failed",
|
|
88
|
+
parseResult.error,
|
|
89
|
+
result,
|
|
89
90
|
);
|
|
90
91
|
}
|
|
91
92
|
|
|
@@ -859,6 +859,42 @@ describe("resolveIntegrationDocumentation", () => {
|
|
|
859
859
|
expect(docs).not.toContain("does not support the `responseType`");
|
|
860
860
|
});
|
|
861
861
|
|
|
862
|
+
it("documents request bodyType only for agents whose sdk-api supports it", async () => {
|
|
863
|
+
const docs = await resolveIntegrationDocumentation("restapiintegration", {
|
|
864
|
+
sdkVersion: "0.0.7",
|
|
865
|
+
});
|
|
866
|
+
|
|
867
|
+
expect(docs).toContain('responseType: "binary"');
|
|
868
|
+
expect(docs).toContain('bodyType: "formUrlencoded"');
|
|
869
|
+
expect(docs).toContain('bodyType: "multipartForm"');
|
|
870
|
+
expect(docs).toContain('bodyType: "binary"');
|
|
871
|
+
expect(docs).not.toContain("does not support the `bodyType`");
|
|
872
|
+
});
|
|
873
|
+
|
|
874
|
+
it("documents binary request bodies with direct raw file bytes", async () => {
|
|
875
|
+
const docs = await resolveIntegrationDocumentation("restapiintegration", {
|
|
876
|
+
sdkVersion: "0.0.7",
|
|
877
|
+
});
|
|
878
|
+
|
|
879
|
+
expect(docs).not.toContain("Buffer.from(raw");
|
|
880
|
+
expect(docs).not.toContain("Buffer.isBuffer(raw");
|
|
881
|
+
expect(docs).toContain(
|
|
882
|
+
'pass the `Uint8Array` returned by `await file.readContentsAsync("raw")` directly as the body',
|
|
883
|
+
);
|
|
884
|
+
expect(docs).toContain('bodyType: "binary"');
|
|
885
|
+
expect(docs).toContain('body: await file.readContentsAsync("raw")');
|
|
886
|
+
});
|
|
887
|
+
|
|
888
|
+
it("notes bodyType non-support before sdk-api request body support", async () => {
|
|
889
|
+
const docs = await resolveIntegrationDocumentation("restapiintegration", {
|
|
890
|
+
sdkVersion: "0.0.6",
|
|
891
|
+
});
|
|
892
|
+
|
|
893
|
+
expect(docs).toContain('responseType: "binary"');
|
|
894
|
+
expect(docs).toContain("does not support the `bodyType` option");
|
|
895
|
+
expect(docs).not.toContain('bodyType: "formUrlencoded"');
|
|
896
|
+
});
|
|
897
|
+
|
|
862
898
|
it("notes non-support instead of documenting responseType on older agents", async () => {
|
|
863
899
|
const docs = await resolveIntegrationDocumentation("restapiintegration", {
|
|
864
900
|
sdkVersion: "0.0.2",
|
|
@@ -869,10 +905,38 @@ describe("resolveIntegrationDocumentation", () => {
|
|
|
869
905
|
expect(docs).not.toContain('responseType: "binary"');
|
|
870
906
|
});
|
|
871
907
|
|
|
872
|
-
it("stays silent about responseType when the agent reports no sdk-api version", async () => {
|
|
908
|
+
it("stays silent about responseType and request bodyType when the agent reports no sdk-api version", async () => {
|
|
873
909
|
const docs = await resolveIntegrationDocumentation("restapiintegration");
|
|
874
910
|
|
|
875
911
|
expect(docs).not.toContain("responseType");
|
|
912
|
+
expect(docs).not.toContain("bodyType");
|
|
913
|
+
});
|
|
914
|
+
});
|
|
915
|
+
|
|
916
|
+
describe("jira attachment gating (real docs)", () => {
|
|
917
|
+
it("gates jira upload docs on sdk-api multipart support", async () => {
|
|
918
|
+
const supported = await resolveIntegrationDocumentation("jira", {
|
|
919
|
+
sdkVersion: "0.0.7",
|
|
920
|
+
});
|
|
921
|
+
expect(supported).toContain('bodyType: "multipartForm"');
|
|
922
|
+
expect(supported).toContain('readContentsAsync("raw")');
|
|
923
|
+
expect(supported).not.toContain("cannot upload");
|
|
924
|
+
|
|
925
|
+
const unsupported = await resolveIntegrationDocumentation("jira", {
|
|
926
|
+
sdkVersion: "0.0.6",
|
|
927
|
+
});
|
|
928
|
+
expect(unsupported).toContain("cannot upload");
|
|
929
|
+
expect(unsupported).not.toContain("bodyType");
|
|
930
|
+
|
|
931
|
+
const unversioned = await resolveIntegrationDocumentation("jira");
|
|
932
|
+
expect(unversioned).not.toContain("bodyType");
|
|
933
|
+
expect(unversioned).not.toContain("cannot upload");
|
|
934
|
+
|
|
935
|
+
for (const docs of [supported, unsupported, unversioned]) {
|
|
936
|
+
expect(docs).not.toContain("body: fileContent");
|
|
937
|
+
expect(docs).not.toContain("file: fileContent");
|
|
938
|
+
expect(docs).not.toContain('"Content-Type": "multipart/form-data"');
|
|
939
|
+
}
|
|
876
940
|
});
|
|
877
941
|
});
|
|
878
942
|
|
|
@@ -118,3 +118,15 @@ describe("SQL integration documentation", () => {
|
|
|
118
118
|
},
|
|
119
119
|
);
|
|
120
120
|
});
|
|
121
|
+
|
|
122
|
+
describe("Anthropic integration documentation", () => {
|
|
123
|
+
it("uses claude-sonnet-5-20250715 instead of Claude 3.5 snapshot IDs", () => {
|
|
124
|
+
const readme = readIntegrationFile("anthropic", "README.md");
|
|
125
|
+
const typeDocs = readIntegrationFile("anthropic", "types.ts");
|
|
126
|
+
|
|
127
|
+
expect(readme).toContain("claude-sonnet-5-20250715");
|
|
128
|
+
expect(typeDocs).toContain("claude-sonnet-5-20250715");
|
|
129
|
+
expect(readme).not.toMatch(/claude-3-5-sonnet-\d{8}/);
|
|
130
|
+
expect(typeDocs).not.toMatch(/claude-3-5-sonnet-\d{8}/);
|
|
131
|
+
});
|
|
132
|
+
});
|