@ai-sdk/provider-utils
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -38,2 +38,3 @@ "use strict"; | ||
createJsonResponseHandler: () => createJsonResponseHandler, | ||
extractResponseHeaders: () => extractResponseHeaders, | ||
generateId: () => generateId, | ||
@@ -53,2 +54,11 @@ getErrorMessage: () => getErrorMessage, | ||
// src/extract-response-headers.ts | ||
function extractResponseHeaders(response) { | ||
const headers = {}; | ||
response.headers.forEach((value, key) => { | ||
headers[key] = value; | ||
}); | ||
return headers; | ||
} | ||
// src/generate-id.ts | ||
@@ -315,11 +325,16 @@ var import_non_secure = require("nanoid/non-secure"); | ||
const responseBody = await response.text(); | ||
const responseHeaders = extractResponseHeaders(response); | ||
if (responseBody.trim() === "") { | ||
return new import_provider5.APICallError({ | ||
message: response.statusText, | ||
url, | ||
requestBodyValues, | ||
statusCode: response.status, | ||
responseBody, | ||
isRetryable: isRetryable == null ? void 0 : isRetryable(response) | ||
}); | ||
return { | ||
responseHeaders, | ||
value: new import_provider5.APICallError({ | ||
message: response.statusText, | ||
url, | ||
requestBodyValues, | ||
statusCode: response.status, | ||
responseHeaders, | ||
responseBody, | ||
isRetryable: isRetryable == null ? void 0 : isRetryable(response) | ||
}) | ||
}; | ||
} | ||
@@ -331,41 +346,53 @@ try { | ||
}); | ||
return new import_provider5.APICallError({ | ||
message: errorToMessage(parsedError), | ||
url, | ||
requestBodyValues, | ||
statusCode: response.status, | ||
responseBody, | ||
data: parsedError, | ||
isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError) | ||
}); | ||
return { | ||
responseHeaders, | ||
value: new import_provider5.APICallError({ | ||
message: errorToMessage(parsedError), | ||
url, | ||
requestBodyValues, | ||
statusCode: response.status, | ||
responseHeaders, | ||
responseBody, | ||
data: parsedError, | ||
isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError) | ||
}) | ||
}; | ||
} catch (parseError) { | ||
return new import_provider5.APICallError({ | ||
message: response.statusText, | ||
url, | ||
requestBodyValues, | ||
statusCode: response.status, | ||
responseBody, | ||
isRetryable: isRetryable == null ? void 0 : isRetryable(response) | ||
}); | ||
return { | ||
responseHeaders, | ||
value: new import_provider5.APICallError({ | ||
message: response.statusText, | ||
url, | ||
requestBodyValues, | ||
statusCode: response.status, | ||
responseHeaders, | ||
responseBody, | ||
isRetryable: isRetryable == null ? void 0 : isRetryable(response) | ||
}) | ||
}; | ||
} | ||
}; | ||
var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) => { | ||
const responseHeaders = extractResponseHeaders(response); | ||
if (response.body == null) { | ||
throw new import_provider5.NoResponseBodyError(); | ||
throw new import_provider5.EmptyResponseBodyError({}); | ||
} | ||
return response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new import_stream.EventSourceParserStream()).pipeThrough( | ||
new TransformStream({ | ||
transform({ data }, controller) { | ||
if (data === "[DONE]") { | ||
return; | ||
return { | ||
responseHeaders, | ||
value: response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new import_stream.EventSourceParserStream()).pipeThrough( | ||
new TransformStream({ | ||
transform({ data }, controller) { | ||
if (data === "[DONE]") { | ||
return; | ||
} | ||
controller.enqueue( | ||
safeParseJSON({ | ||
text: data, | ||
schema: chunkSchema | ||
}) | ||
); | ||
} | ||
controller.enqueue( | ||
safeParseJSON({ | ||
text: data, | ||
schema: chunkSchema | ||
}) | ||
); | ||
} | ||
}) | ||
); | ||
}) | ||
) | ||
}; | ||
}; | ||
@@ -378,2 +405,3 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requestBodyValues }) => { | ||
}); | ||
const responseHeaders = extractResponseHeaders(response); | ||
if (!parsedResult.success) { | ||
@@ -384,2 +412,3 @@ throw new import_provider5.APICallError({ | ||
statusCode: response.status, | ||
responseHeaders, | ||
responseBody, | ||
@@ -390,3 +419,6 @@ url, | ||
} | ||
return parsedResult.value; | ||
return { | ||
responseHeaders, | ||
value: parsedResult.value | ||
}; | ||
}; | ||
@@ -419,2 +451,3 @@ | ||
createJsonResponseHandler, | ||
extractResponseHeaders, | ||
generateId, | ||
@@ -421,0 +454,0 @@ getErrorMessage, |
@@ -6,2 +6,10 @@ import { JSONParseError, TypeValidationError, APICallError } from '@ai-sdk/provider'; | ||
/** | ||
Extracts the headers from a response object and returns them as a key-value object. | ||
@param response - The response object to extract headers from. | ||
@returns The headers as a key-value object. | ||
*/ | ||
declare function extractResponseHeaders(response: Response): Record<string, string>; | ||
/** | ||
* Generates a 7-character random string to use for IDs. Not secure. | ||
@@ -75,3 +83,6 @@ */ | ||
response: Response; | ||
}) => PromiseLike<RETURN_TYPE>; | ||
}) => PromiseLike<{ | ||
value: RETURN_TYPE; | ||
responseHeaders?: Record<string, string>; | ||
}>; | ||
declare const createJsonErrorResponseHandler: <T>({ errorSchema, errorToMessage, isRetryable, }: { | ||
@@ -92,3 +103,6 @@ errorSchema: ZodSchema<T, zod.ZodTypeDef, T>; | ||
abortSignal?: AbortSignal | undefined; | ||
}) => Promise<T>; | ||
}) => Promise<{ | ||
value: T; | ||
responseHeaders?: Record<string, string> | undefined; | ||
}>; | ||
declare const postToApi: <T>({ url, headers, body, successfulResponseHandler, failedResponseHandler, abortSignal, }: { | ||
@@ -104,3 +118,6 @@ url: string; | ||
abortSignal?: AbortSignal | undefined; | ||
}) => Promise<T>; | ||
}) => Promise<{ | ||
value: T; | ||
responseHeaders?: Record<string, string> | undefined; | ||
}>; | ||
@@ -145,2 +162,2 @@ declare function convertBase64ToUint8Array(base64String: string): Uint8Array; | ||
export { type ParseResult, type ResponseHandler, convertBase64ToUint8Array, convertUint8ArrayToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, generateId, getErrorMessage, isParseableJson, loadApiKey, parseJSON, postJsonToApi, postToApi, safeParseJSON, safeValidateTypes, validateTypes, withoutTrailingSlash }; | ||
export { type ParseResult, type ResponseHandler, convertBase64ToUint8Array, convertUint8ArrayToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, extractResponseHeaders, generateId, getErrorMessage, isParseableJson, loadApiKey, parseJSON, postJsonToApi, postToApi, safeParseJSON, safeValidateTypes, validateTypes, withoutTrailingSlash }; |
@@ -38,2 +38,3 @@ "use strict"; | ||
createJsonResponseHandler: () => createJsonResponseHandler, | ||
extractResponseHeaders: () => extractResponseHeaders, | ||
generateId: () => generateId, | ||
@@ -53,2 +54,11 @@ getErrorMessage: () => getErrorMessage, | ||
// src/extract-response-headers.ts | ||
function extractResponseHeaders(response) { | ||
const headers = {}; | ||
response.headers.forEach((value, key) => { | ||
headers[key] = value; | ||
}); | ||
return headers; | ||
} | ||
// src/generate-id.ts | ||
@@ -315,11 +325,16 @@ var import_non_secure = require("nanoid/non-secure"); | ||
const responseBody = await response.text(); | ||
const responseHeaders = extractResponseHeaders(response); | ||
if (responseBody.trim() === "") { | ||
return new import_provider5.APICallError({ | ||
message: response.statusText, | ||
url, | ||
requestBodyValues, | ||
statusCode: response.status, | ||
responseBody, | ||
isRetryable: isRetryable == null ? void 0 : isRetryable(response) | ||
}); | ||
return { | ||
responseHeaders, | ||
value: new import_provider5.APICallError({ | ||
message: response.statusText, | ||
url, | ||
requestBodyValues, | ||
statusCode: response.status, | ||
responseHeaders, | ||
responseBody, | ||
isRetryable: isRetryable == null ? void 0 : isRetryable(response) | ||
}) | ||
}; | ||
} | ||
@@ -331,41 +346,53 @@ try { | ||
}); | ||
return new import_provider5.APICallError({ | ||
message: errorToMessage(parsedError), | ||
url, | ||
requestBodyValues, | ||
statusCode: response.status, | ||
responseBody, | ||
data: parsedError, | ||
isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError) | ||
}); | ||
return { | ||
responseHeaders, | ||
value: new import_provider5.APICallError({ | ||
message: errorToMessage(parsedError), | ||
url, | ||
requestBodyValues, | ||
statusCode: response.status, | ||
responseHeaders, | ||
responseBody, | ||
data: parsedError, | ||
isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError) | ||
}) | ||
}; | ||
} catch (parseError) { | ||
return new import_provider5.APICallError({ | ||
message: response.statusText, | ||
url, | ||
requestBodyValues, | ||
statusCode: response.status, | ||
responseBody, | ||
isRetryable: isRetryable == null ? void 0 : isRetryable(response) | ||
}); | ||
return { | ||
responseHeaders, | ||
value: new import_provider5.APICallError({ | ||
message: response.statusText, | ||
url, | ||
requestBodyValues, | ||
statusCode: response.status, | ||
responseHeaders, | ||
responseBody, | ||
isRetryable: isRetryable == null ? void 0 : isRetryable(response) | ||
}) | ||
}; | ||
} | ||
}; | ||
var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) => { | ||
const responseHeaders = extractResponseHeaders(response); | ||
if (response.body == null) { | ||
throw new import_provider5.NoResponseBodyError(); | ||
throw new import_provider5.EmptyResponseBodyError({}); | ||
} | ||
return response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new import_stream.EventSourceParserStream()).pipeThrough( | ||
new TransformStream({ | ||
transform({ data }, controller) { | ||
if (data === "[DONE]") { | ||
return; | ||
return { | ||
responseHeaders, | ||
value: response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new import_stream.EventSourceParserStream()).pipeThrough( | ||
new TransformStream({ | ||
transform({ data }, controller) { | ||
if (data === "[DONE]") { | ||
return; | ||
} | ||
controller.enqueue( | ||
safeParseJSON({ | ||
text: data, | ||
schema: chunkSchema | ||
}) | ||
); | ||
} | ||
controller.enqueue( | ||
safeParseJSON({ | ||
text: data, | ||
schema: chunkSchema | ||
}) | ||
); | ||
} | ||
}) | ||
); | ||
}) | ||
) | ||
}; | ||
}; | ||
@@ -378,2 +405,3 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requestBodyValues }) => { | ||
}); | ||
const responseHeaders = extractResponseHeaders(response); | ||
if (!parsedResult.success) { | ||
@@ -384,2 +412,3 @@ throw new import_provider5.APICallError({ | ||
statusCode: response.status, | ||
responseHeaders, | ||
responseBody, | ||
@@ -390,3 +419,6 @@ url, | ||
} | ||
return parsedResult.value; | ||
return { | ||
responseHeaders, | ||
value: parsedResult.value | ||
}; | ||
}; | ||
@@ -419,2 +451,3 @@ | ||
createJsonResponseHandler, | ||
extractResponseHeaders, | ||
generateId, | ||
@@ -421,0 +454,0 @@ getErrorMessage, |
{ | ||
"name": "@ai-sdk/provider-utils", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"license": "Apache-2.0", | ||
@@ -28,3 +28,3 @@ "sideEffects": false, | ||
"dependencies": { | ||
"@ai-sdk/provider": "0.0.1", | ||
"@ai-sdk/provider": "0.0.2", | ||
"eventsource-parser": "1.1.2", | ||
@@ -31,0 +31,0 @@ "nanoid": "3.3.6", |
@@ -7,2 +7,3 @@ import { SetupServer } from 'msw/node'; | ||
readonly server: SetupServer; | ||
responseHeaders: Record<string, string>; | ||
responseBodyJson: any; | ||
@@ -18,2 +19,3 @@ request: Request | undefined; | ||
readonly server: SetupServer; | ||
responseHeaders: Record<string, string>; | ||
responseChunks: any[]; | ||
@@ -20,0 +22,0 @@ request: Request | undefined; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1636777
17879
+ Added@ai-sdk/provider@0.0.2(transitive)
- Removed@ai-sdk/provider@0.0.1(transitive)
Updated@ai-sdk/provider@0.0.2