@bagelink/sdk 1.8.32 → 1.8.35
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 +17 -0
- package/dist/index.cjs +22 -3
- package/dist/index.d.cts +28 -2
- package/dist/index.d.mts +28 -2
- package/dist/index.d.ts +28 -2
- package/dist/index.mjs +22 -4
- package/package.json +1 -1
- package/src/index.ts +7 -0
- package/src/openAPITools/functionGenerator.ts +10 -3
- package/src/unwrap.ts +41 -0
package/README.md
CHANGED
|
@@ -93,3 +93,20 @@ const { loading, error, data, execute } = useApiRequest(
|
|
|
93
93
|
)
|
|
94
94
|
```
|
|
95
95
|
|
|
96
|
+
## Type Assignment
|
|
97
|
+
|
|
98
|
+
For assigning single objects to plain types:
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { unwrap } from './.bagelink/api'
|
|
102
|
+
|
|
103
|
+
const agent = ref<AgentResponse | null>(null)
|
|
104
|
+
agent.value = unwrap(await api.agents.getByAgentId({ agentId }))
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Arrays work without `unwrap()`:
|
|
108
|
+
```typescript
|
|
109
|
+
const agents = ref<AgentResponse[]>([])
|
|
110
|
+
agents.value = await api.agents.get() // Works fine
|
|
111
|
+
```
|
|
112
|
+
|
package/dist/index.cjs
CHANGED
|
@@ -727,7 +727,7 @@ function generateFunctionForOperation(method, path, operation) {
|
|
|
727
727
|
);
|
|
728
728
|
const allParams = isFileUpload ? "file: File, options?: UploadOptions & { dirPath?: string, tags?: string[] }" : combineAllParams(parameters, requestBodyParam);
|
|
729
729
|
const responseType = generateResponseType(operation.responses);
|
|
730
|
-
const responseTypeStr = responseType ? `: Promise<
|
|
730
|
+
const responseTypeStr = responseType ? `: Promise<ApiResponse<${responseType}>>` : "";
|
|
731
731
|
const functionComment = buildJSDocComment(operation, method, path);
|
|
732
732
|
if (isStream) {
|
|
733
733
|
const eventTypes = extractSSEEventTypes(operation);
|
|
@@ -930,6 +930,7 @@ import {
|
|
|
930
930
|
formatFieldErrors,
|
|
931
931
|
useApiRequest,
|
|
932
932
|
useCancellableRequest,
|
|
933
|
+
unwrap,
|
|
933
934
|
${streamImports}
|
|
934
935
|
type ApiConfig,
|
|
935
936
|
type ParsedError,
|
|
@@ -938,7 +939,8 @@ import {
|
|
|
938
939
|
type ResponseMetadata,
|
|
939
940
|
type ApiRequestState,
|
|
940
941
|
type RequestInterceptor,
|
|
941
|
-
type ResponseInterceptor
|
|
942
|
+
type ResponseInterceptor,
|
|
943
|
+
type Unwrapped
|
|
942
944
|
} from '@bagelink/sdk';
|
|
943
945
|
import { ref, type Ref } from 'vue';
|
|
944
946
|
|
|
@@ -1092,6 +1094,7 @@ export {
|
|
|
1092
1094
|
formatFieldErrors,
|
|
1093
1095
|
useApiRequest,
|
|
1094
1096
|
useCancellableRequest,
|
|
1097
|
+
unwrap,
|
|
1095
1098
|
ApiResponse,
|
|
1096
1099
|
type ApiConfig,
|
|
1097
1100
|
type ParsedError,
|
|
@@ -1100,7 +1103,8 @@ export {
|
|
|
1100
1103
|
type FastAPIValidationError,
|
|
1101
1104
|
type FastAPIErrorResponse,
|
|
1102
1105
|
type RequestInterceptor,
|
|
1103
|
-
type ResponseInterceptor
|
|
1106
|
+
type ResponseInterceptor,
|
|
1107
|
+
type Unwrapped
|
|
1104
1108
|
}
|
|
1105
1109
|
`;
|
|
1106
1110
|
return templateCode.replace(/"([^"]+)":/g, "$1:");
|
|
@@ -1389,6 +1393,20 @@ const index = async (openApiUrl, baseUrl) => {
|
|
|
1389
1393
|
}
|
|
1390
1394
|
};
|
|
1391
1395
|
|
|
1396
|
+
function unwrap(response) {
|
|
1397
|
+
if (!response || typeof response !== "object") {
|
|
1398
|
+
return response;
|
|
1399
|
+
}
|
|
1400
|
+
if (Array.isArray(response)) {
|
|
1401
|
+
return response;
|
|
1402
|
+
}
|
|
1403
|
+
if ("$raw" in response) {
|
|
1404
|
+
const { $raw, $metadata, totalCount, page, perPage, totalPages, rateLimit, rateLimitReset, getHeader, hasPagination, hasNextPage, hasPrevPage, ...data } = response;
|
|
1405
|
+
return data;
|
|
1406
|
+
}
|
|
1407
|
+
return response;
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1392
1410
|
function formatAPIErrorMessage(err) {
|
|
1393
1411
|
const error = err;
|
|
1394
1412
|
if (!error || !error.response) {
|
|
@@ -1919,6 +1937,7 @@ exports.isSSEStream = isSSEStream;
|
|
|
1919
1937
|
exports.isSchemaObject = isSchemaObject;
|
|
1920
1938
|
exports.openAPI = index;
|
|
1921
1939
|
exports.parseApiError = parseApiError;
|
|
1940
|
+
exports.unwrap = unwrap;
|
|
1922
1941
|
exports.useApiRequest = useApiRequest;
|
|
1923
1942
|
exports.useCancellableRequest = useCancellableRequest;
|
|
1924
1943
|
exports.wrapApiForDirectReturn = wrapApiForDirectReturn;
|
package/dist/index.d.cts
CHANGED
|
@@ -532,6 +532,27 @@ interface OpenAPIResponse {
|
|
|
532
532
|
}
|
|
533
533
|
declare const _default: (openApiUrl: string, baseUrl: string) => Promise<OpenAPIResponse>;
|
|
534
534
|
|
|
535
|
+
/**
|
|
536
|
+
* Unwrap utility for ApiResponse
|
|
537
|
+
*
|
|
538
|
+
* Use this when you need to assign API responses to plain types
|
|
539
|
+
*/
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Unwrap an ApiResponse to get the plain data
|
|
543
|
+
*
|
|
544
|
+
* @example
|
|
545
|
+
* import { unwrap } from '@bagelink/sdk'
|
|
546
|
+
*
|
|
547
|
+
* const agent = ref<AgentResponse | null>(null)
|
|
548
|
+
* agent.value = unwrap(await api.agents.getByAgentId({ agentId }))
|
|
549
|
+
*/
|
|
550
|
+
declare function unwrap<T>(response: ApiResponse<T> | T): T;
|
|
551
|
+
/**
|
|
552
|
+
* Type helper to unwrap ApiResponse
|
|
553
|
+
*/
|
|
554
|
+
type Unwrapped<T> = T extends ApiResponse<infer U> ? U : T;
|
|
555
|
+
|
|
535
556
|
declare function formatAPIErrorMessage(err: unknown): string;
|
|
536
557
|
|
|
537
558
|
/**
|
|
@@ -553,6 +574,11 @@ interface ResponseMetadata {
|
|
|
553
574
|
/** Custom headers */
|
|
554
575
|
[key: string]: any;
|
|
555
576
|
}
|
|
577
|
+
/**
|
|
578
|
+
* Helper to unwrap ApiResponse type for assignments
|
|
579
|
+
* Use this when you need to assign to a plain type
|
|
580
|
+
*/
|
|
581
|
+
type Unwrap<T> = T extends ApiResponse<infer U> ? U : T;
|
|
556
582
|
/**
|
|
557
583
|
* Enhanced API response that adds metadata to the response data
|
|
558
584
|
* - For arrays: acts as array with metadata properties
|
|
@@ -772,5 +798,5 @@ declare class Bagel {
|
|
|
772
798
|
uploadFile<T>(file: File, options?: UploadOptions): Promise<T>;
|
|
773
799
|
}
|
|
774
800
|
|
|
775
|
-
export { ApiResponse, Bagel, StreamController, createSSEStream, createSSEStreamPost, dereference, extractSSEEventInfo, extractSSEEventTypes, formatAPIErrorMessage, formatFieldErrors, getPath, isReferenceObject, isSSEStream, isSchemaObject, _default as openAPI, parseApiError, useApiRequest, useCancellableRequest, wrapApiForDirectReturn };
|
|
776
|
-
export type { ApiConfig, ApiRequestState, BaseParameterObject, CallbackObject, CallbacksObject, ComponentsObject, ContactObject, ContentObject, DiscriminatorObject, EncodingObject, EncodingPropertyObject, ExampleObject, ExamplesObject, ExternalDocumentationObject, FastAPIErrorResponse, FastAPIValidationError, HeaderObject, HeadersObject, InfoObject, LicenseObject, LinkObject, LinkParametersObject, LinksObject, MediaTypeObject, OAuthFlowObject, OAuthFlowsObject, OpenAPIObject, OperationObject, ParameterLocation, ParameterObject, ParameterStyle, ParsedError, PathItemObject, PathsObject, ReferenceObject, RequestBodyObject, RequestInterceptor, ResponseInterceptor, ResponseMetadata, ResponseObject, ResponsesObject, SSEEvent, SSEEventTypeInfo, SSEStreamOptions, SchemaObject, SchemaObjectType, SchemasObject, ScopesObject, SecurityRequirementObject, SecuritySchemeObject, SecuritySchemeType, ServerObject, ServerVariableObject, StreamEventMap, TableToTypeMapping, Tables, TagObject, TypeFormatT, UploadOptions, User, XmlObject };
|
|
801
|
+
export { ApiResponse, Bagel, StreamController, createSSEStream, createSSEStreamPost, dereference, extractSSEEventInfo, extractSSEEventTypes, formatAPIErrorMessage, formatFieldErrors, getPath, isReferenceObject, isSSEStream, isSchemaObject, _default as openAPI, parseApiError, unwrap, useApiRequest, useCancellableRequest, wrapApiForDirectReturn };
|
|
802
|
+
export type { ApiConfig, ApiRequestState, BaseParameterObject, CallbackObject, CallbacksObject, ComponentsObject, ContactObject, ContentObject, DiscriminatorObject, EncodingObject, EncodingPropertyObject, ExampleObject, ExamplesObject, ExternalDocumentationObject, FastAPIErrorResponse, FastAPIValidationError, HeaderObject, HeadersObject, InfoObject, LicenseObject, LinkObject, LinkParametersObject, LinksObject, MediaTypeObject, OAuthFlowObject, OAuthFlowsObject, OpenAPIObject, OperationObject, ParameterLocation, ParameterObject, ParameterStyle, ParsedError, PathItemObject, PathsObject, ReferenceObject, RequestBodyObject, RequestInterceptor, ResponseInterceptor, ResponseMetadata, ResponseObject, ResponsesObject, SSEEvent, SSEEventTypeInfo, SSEStreamOptions, SchemaObject, SchemaObjectType, SchemasObject, ScopesObject, SecurityRequirementObject, SecuritySchemeObject, SecuritySchemeType, ServerObject, ServerVariableObject, StreamEventMap, TableToTypeMapping, Tables, TagObject, TypeFormatT, Unwrap, Unwrapped, UploadOptions, User, XmlObject };
|
package/dist/index.d.mts
CHANGED
|
@@ -532,6 +532,27 @@ interface OpenAPIResponse {
|
|
|
532
532
|
}
|
|
533
533
|
declare const _default: (openApiUrl: string, baseUrl: string) => Promise<OpenAPIResponse>;
|
|
534
534
|
|
|
535
|
+
/**
|
|
536
|
+
* Unwrap utility for ApiResponse
|
|
537
|
+
*
|
|
538
|
+
* Use this when you need to assign API responses to plain types
|
|
539
|
+
*/
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Unwrap an ApiResponse to get the plain data
|
|
543
|
+
*
|
|
544
|
+
* @example
|
|
545
|
+
* import { unwrap } from '@bagelink/sdk'
|
|
546
|
+
*
|
|
547
|
+
* const agent = ref<AgentResponse | null>(null)
|
|
548
|
+
* agent.value = unwrap(await api.agents.getByAgentId({ agentId }))
|
|
549
|
+
*/
|
|
550
|
+
declare function unwrap<T>(response: ApiResponse<T> | T): T;
|
|
551
|
+
/**
|
|
552
|
+
* Type helper to unwrap ApiResponse
|
|
553
|
+
*/
|
|
554
|
+
type Unwrapped<T> = T extends ApiResponse<infer U> ? U : T;
|
|
555
|
+
|
|
535
556
|
declare function formatAPIErrorMessage(err: unknown): string;
|
|
536
557
|
|
|
537
558
|
/**
|
|
@@ -553,6 +574,11 @@ interface ResponseMetadata {
|
|
|
553
574
|
/** Custom headers */
|
|
554
575
|
[key: string]: any;
|
|
555
576
|
}
|
|
577
|
+
/**
|
|
578
|
+
* Helper to unwrap ApiResponse type for assignments
|
|
579
|
+
* Use this when you need to assign to a plain type
|
|
580
|
+
*/
|
|
581
|
+
type Unwrap<T> = T extends ApiResponse<infer U> ? U : T;
|
|
556
582
|
/**
|
|
557
583
|
* Enhanced API response that adds metadata to the response data
|
|
558
584
|
* - For arrays: acts as array with metadata properties
|
|
@@ -772,5 +798,5 @@ declare class Bagel {
|
|
|
772
798
|
uploadFile<T>(file: File, options?: UploadOptions): Promise<T>;
|
|
773
799
|
}
|
|
774
800
|
|
|
775
|
-
export { ApiResponse, Bagel, StreamController, createSSEStream, createSSEStreamPost, dereference, extractSSEEventInfo, extractSSEEventTypes, formatAPIErrorMessage, formatFieldErrors, getPath, isReferenceObject, isSSEStream, isSchemaObject, _default as openAPI, parseApiError, useApiRequest, useCancellableRequest, wrapApiForDirectReturn };
|
|
776
|
-
export type { ApiConfig, ApiRequestState, BaseParameterObject, CallbackObject, CallbacksObject, ComponentsObject, ContactObject, ContentObject, DiscriminatorObject, EncodingObject, EncodingPropertyObject, ExampleObject, ExamplesObject, ExternalDocumentationObject, FastAPIErrorResponse, FastAPIValidationError, HeaderObject, HeadersObject, InfoObject, LicenseObject, LinkObject, LinkParametersObject, LinksObject, MediaTypeObject, OAuthFlowObject, OAuthFlowsObject, OpenAPIObject, OperationObject, ParameterLocation, ParameterObject, ParameterStyle, ParsedError, PathItemObject, PathsObject, ReferenceObject, RequestBodyObject, RequestInterceptor, ResponseInterceptor, ResponseMetadata, ResponseObject, ResponsesObject, SSEEvent, SSEEventTypeInfo, SSEStreamOptions, SchemaObject, SchemaObjectType, SchemasObject, ScopesObject, SecurityRequirementObject, SecuritySchemeObject, SecuritySchemeType, ServerObject, ServerVariableObject, StreamEventMap, TableToTypeMapping, Tables, TagObject, TypeFormatT, UploadOptions, User, XmlObject };
|
|
801
|
+
export { ApiResponse, Bagel, StreamController, createSSEStream, createSSEStreamPost, dereference, extractSSEEventInfo, extractSSEEventTypes, formatAPIErrorMessage, formatFieldErrors, getPath, isReferenceObject, isSSEStream, isSchemaObject, _default as openAPI, parseApiError, unwrap, useApiRequest, useCancellableRequest, wrapApiForDirectReturn };
|
|
802
|
+
export type { ApiConfig, ApiRequestState, BaseParameterObject, CallbackObject, CallbacksObject, ComponentsObject, ContactObject, ContentObject, DiscriminatorObject, EncodingObject, EncodingPropertyObject, ExampleObject, ExamplesObject, ExternalDocumentationObject, FastAPIErrorResponse, FastAPIValidationError, HeaderObject, HeadersObject, InfoObject, LicenseObject, LinkObject, LinkParametersObject, LinksObject, MediaTypeObject, OAuthFlowObject, OAuthFlowsObject, OpenAPIObject, OperationObject, ParameterLocation, ParameterObject, ParameterStyle, ParsedError, PathItemObject, PathsObject, ReferenceObject, RequestBodyObject, RequestInterceptor, ResponseInterceptor, ResponseMetadata, ResponseObject, ResponsesObject, SSEEvent, SSEEventTypeInfo, SSEStreamOptions, SchemaObject, SchemaObjectType, SchemasObject, ScopesObject, SecurityRequirementObject, SecuritySchemeObject, SecuritySchemeType, ServerObject, ServerVariableObject, StreamEventMap, TableToTypeMapping, Tables, TagObject, TypeFormatT, Unwrap, Unwrapped, UploadOptions, User, XmlObject };
|
package/dist/index.d.ts
CHANGED
|
@@ -532,6 +532,27 @@ interface OpenAPIResponse {
|
|
|
532
532
|
}
|
|
533
533
|
declare const _default: (openApiUrl: string, baseUrl: string) => Promise<OpenAPIResponse>;
|
|
534
534
|
|
|
535
|
+
/**
|
|
536
|
+
* Unwrap utility for ApiResponse
|
|
537
|
+
*
|
|
538
|
+
* Use this when you need to assign API responses to plain types
|
|
539
|
+
*/
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Unwrap an ApiResponse to get the plain data
|
|
543
|
+
*
|
|
544
|
+
* @example
|
|
545
|
+
* import { unwrap } from '@bagelink/sdk'
|
|
546
|
+
*
|
|
547
|
+
* const agent = ref<AgentResponse | null>(null)
|
|
548
|
+
* agent.value = unwrap(await api.agents.getByAgentId({ agentId }))
|
|
549
|
+
*/
|
|
550
|
+
declare function unwrap<T>(response: ApiResponse<T> | T): T;
|
|
551
|
+
/**
|
|
552
|
+
* Type helper to unwrap ApiResponse
|
|
553
|
+
*/
|
|
554
|
+
type Unwrapped<T> = T extends ApiResponse<infer U> ? U : T;
|
|
555
|
+
|
|
535
556
|
declare function formatAPIErrorMessage(err: unknown): string;
|
|
536
557
|
|
|
537
558
|
/**
|
|
@@ -553,6 +574,11 @@ interface ResponseMetadata {
|
|
|
553
574
|
/** Custom headers */
|
|
554
575
|
[key: string]: any;
|
|
555
576
|
}
|
|
577
|
+
/**
|
|
578
|
+
* Helper to unwrap ApiResponse type for assignments
|
|
579
|
+
* Use this when you need to assign to a plain type
|
|
580
|
+
*/
|
|
581
|
+
type Unwrap<T> = T extends ApiResponse<infer U> ? U : T;
|
|
556
582
|
/**
|
|
557
583
|
* Enhanced API response that adds metadata to the response data
|
|
558
584
|
* - For arrays: acts as array with metadata properties
|
|
@@ -772,5 +798,5 @@ declare class Bagel {
|
|
|
772
798
|
uploadFile<T>(file: File, options?: UploadOptions): Promise<T>;
|
|
773
799
|
}
|
|
774
800
|
|
|
775
|
-
export { ApiResponse, Bagel, StreamController, createSSEStream, createSSEStreamPost, dereference, extractSSEEventInfo, extractSSEEventTypes, formatAPIErrorMessage, formatFieldErrors, getPath, isReferenceObject, isSSEStream, isSchemaObject, _default as openAPI, parseApiError, useApiRequest, useCancellableRequest, wrapApiForDirectReturn };
|
|
776
|
-
export type { ApiConfig, ApiRequestState, BaseParameterObject, CallbackObject, CallbacksObject, ComponentsObject, ContactObject, ContentObject, DiscriminatorObject, EncodingObject, EncodingPropertyObject, ExampleObject, ExamplesObject, ExternalDocumentationObject, FastAPIErrorResponse, FastAPIValidationError, HeaderObject, HeadersObject, InfoObject, LicenseObject, LinkObject, LinkParametersObject, LinksObject, MediaTypeObject, OAuthFlowObject, OAuthFlowsObject, OpenAPIObject, OperationObject, ParameterLocation, ParameterObject, ParameterStyle, ParsedError, PathItemObject, PathsObject, ReferenceObject, RequestBodyObject, RequestInterceptor, ResponseInterceptor, ResponseMetadata, ResponseObject, ResponsesObject, SSEEvent, SSEEventTypeInfo, SSEStreamOptions, SchemaObject, SchemaObjectType, SchemasObject, ScopesObject, SecurityRequirementObject, SecuritySchemeObject, SecuritySchemeType, ServerObject, ServerVariableObject, StreamEventMap, TableToTypeMapping, Tables, TagObject, TypeFormatT, UploadOptions, User, XmlObject };
|
|
801
|
+
export { ApiResponse, Bagel, StreamController, createSSEStream, createSSEStreamPost, dereference, extractSSEEventInfo, extractSSEEventTypes, formatAPIErrorMessage, formatFieldErrors, getPath, isReferenceObject, isSSEStream, isSchemaObject, _default as openAPI, parseApiError, unwrap, useApiRequest, useCancellableRequest, wrapApiForDirectReturn };
|
|
802
|
+
export type { ApiConfig, ApiRequestState, BaseParameterObject, CallbackObject, CallbacksObject, ComponentsObject, ContactObject, ContentObject, DiscriminatorObject, EncodingObject, EncodingPropertyObject, ExampleObject, ExamplesObject, ExternalDocumentationObject, FastAPIErrorResponse, FastAPIValidationError, HeaderObject, HeadersObject, InfoObject, LicenseObject, LinkObject, LinkParametersObject, LinksObject, MediaTypeObject, OAuthFlowObject, OAuthFlowsObject, OpenAPIObject, OperationObject, ParameterLocation, ParameterObject, ParameterStyle, ParsedError, PathItemObject, PathsObject, ReferenceObject, RequestBodyObject, RequestInterceptor, ResponseInterceptor, ResponseMetadata, ResponseObject, ResponsesObject, SSEEvent, SSEEventTypeInfo, SSEStreamOptions, SchemaObject, SchemaObjectType, SchemasObject, ScopesObject, SecurityRequirementObject, SecuritySchemeObject, SecuritySchemeType, ServerObject, ServerVariableObject, StreamEventMap, TableToTypeMapping, Tables, TagObject, TypeFormatT, Unwrap, Unwrapped, UploadOptions, User, XmlObject };
|
package/dist/index.mjs
CHANGED
|
@@ -721,7 +721,7 @@ function generateFunctionForOperation(method, path, operation) {
|
|
|
721
721
|
);
|
|
722
722
|
const allParams = isFileUpload ? "file: File, options?: UploadOptions & { dirPath?: string, tags?: string[] }" : combineAllParams(parameters, requestBodyParam);
|
|
723
723
|
const responseType = generateResponseType(operation.responses);
|
|
724
|
-
const responseTypeStr = responseType ? `: Promise<
|
|
724
|
+
const responseTypeStr = responseType ? `: Promise<ApiResponse<${responseType}>>` : "";
|
|
725
725
|
const functionComment = buildJSDocComment(operation, method, path);
|
|
726
726
|
if (isStream) {
|
|
727
727
|
const eventTypes = extractSSEEventTypes(operation);
|
|
@@ -924,6 +924,7 @@ import {
|
|
|
924
924
|
formatFieldErrors,
|
|
925
925
|
useApiRequest,
|
|
926
926
|
useCancellableRequest,
|
|
927
|
+
unwrap,
|
|
927
928
|
${streamImports}
|
|
928
929
|
type ApiConfig,
|
|
929
930
|
type ParsedError,
|
|
@@ -932,7 +933,8 @@ import {
|
|
|
932
933
|
type ResponseMetadata,
|
|
933
934
|
type ApiRequestState,
|
|
934
935
|
type RequestInterceptor,
|
|
935
|
-
type ResponseInterceptor
|
|
936
|
+
type ResponseInterceptor,
|
|
937
|
+
type Unwrapped
|
|
936
938
|
} from '@bagelink/sdk';
|
|
937
939
|
import { ref, type Ref } from 'vue';
|
|
938
940
|
|
|
@@ -1086,6 +1088,7 @@ export {
|
|
|
1086
1088
|
formatFieldErrors,
|
|
1087
1089
|
useApiRequest,
|
|
1088
1090
|
useCancellableRequest,
|
|
1091
|
+
unwrap,
|
|
1089
1092
|
ApiResponse,
|
|
1090
1093
|
type ApiConfig,
|
|
1091
1094
|
type ParsedError,
|
|
@@ -1094,7 +1097,8 @@ export {
|
|
|
1094
1097
|
type FastAPIValidationError,
|
|
1095
1098
|
type FastAPIErrorResponse,
|
|
1096
1099
|
type RequestInterceptor,
|
|
1097
|
-
type ResponseInterceptor
|
|
1100
|
+
type ResponseInterceptor,
|
|
1101
|
+
type Unwrapped
|
|
1098
1102
|
}
|
|
1099
1103
|
`;
|
|
1100
1104
|
return templateCode.replace(/"([^"]+)":/g, "$1:");
|
|
@@ -1383,6 +1387,20 @@ const index = async (openApiUrl, baseUrl) => {
|
|
|
1383
1387
|
}
|
|
1384
1388
|
};
|
|
1385
1389
|
|
|
1390
|
+
function unwrap(response) {
|
|
1391
|
+
if (!response || typeof response !== "object") {
|
|
1392
|
+
return response;
|
|
1393
|
+
}
|
|
1394
|
+
if (Array.isArray(response)) {
|
|
1395
|
+
return response;
|
|
1396
|
+
}
|
|
1397
|
+
if ("$raw" in response) {
|
|
1398
|
+
const { $raw, $metadata, totalCount, page, perPage, totalPages, rateLimit, rateLimitReset, getHeader, hasPagination, hasNextPage, hasPrevPage, ...data } = response;
|
|
1399
|
+
return data;
|
|
1400
|
+
}
|
|
1401
|
+
return response;
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1386
1404
|
function formatAPIErrorMessage(err) {
|
|
1387
1405
|
const error = err;
|
|
1388
1406
|
if (!error || !error.response) {
|
|
@@ -1897,4 +1915,4 @@ class Bagel {
|
|
|
1897
1915
|
}
|
|
1898
1916
|
}
|
|
1899
1917
|
|
|
1900
|
-
export { ApiResponse, Bagel, StreamController, createSSEStream, createSSEStreamPost, dereference, extractSSEEventInfo, extractSSEEventTypes, formatAPIErrorMessage, formatFieldErrors, getPath, isReferenceObject, isSSEStream, isSchemaObject, index as openAPI, parseApiError, useApiRequest, useCancellableRequest, wrapApiForDirectReturn };
|
|
1918
|
+
export { ApiResponse, Bagel, StreamController, createSSEStream, createSSEStreamPost, dereference, extractSSEEventInfo, extractSSEEventTypes, formatAPIErrorMessage, formatFieldErrors, getPath, isReferenceObject, isSSEStream, isSchemaObject, index as openAPI, parseApiError, unwrap, useApiRequest, useCancellableRequest, wrapApiForDirectReturn };
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -13,6 +13,7 @@ export {
|
|
|
13
13
|
StreamController,
|
|
14
14
|
type StreamEventMap
|
|
15
15
|
} from './openAPITools/streamClient'
|
|
16
|
+
export { unwrap, type Unwrapped } from './unwrap'
|
|
16
17
|
|
|
17
18
|
// ============================================================================
|
|
18
19
|
// Re-export ApiResponse and Utilities (for generated code)
|
|
@@ -38,6 +39,12 @@ export interface ResponseMetadata {
|
|
|
38
39
|
[key: string]: any
|
|
39
40
|
}
|
|
40
41
|
|
|
42
|
+
/**
|
|
43
|
+
* Helper to unwrap ApiResponse type for assignments
|
|
44
|
+
* Use this when you need to assign to a plain type
|
|
45
|
+
*/
|
|
46
|
+
export type Unwrap<T> = T extends ApiResponse<infer U> ? U : T
|
|
47
|
+
|
|
41
48
|
/**
|
|
42
49
|
* Enhanced API response that adds metadata to the response data
|
|
43
50
|
* - For arrays: acts as array with metadata properties
|
|
@@ -629,8 +629,11 @@ function generateFunctionForOperation(
|
|
|
629
629
|
: combineAllParams(parameters, requestBodyParam)
|
|
630
630
|
|
|
631
631
|
const responseType = generateResponseType(operation.responses)
|
|
632
|
+
|
|
633
|
+
// Always use ApiResponse wrapper for type consistency
|
|
634
|
+
// The create() method will handle single objects vs arrays at runtime
|
|
632
635
|
const responseTypeStr = responseType
|
|
633
|
-
? `: Promise<
|
|
636
|
+
? `: Promise<ApiResponse<${responseType}>>`
|
|
634
637
|
: ''
|
|
635
638
|
|
|
636
639
|
// Create JSDoc comment with OpenAPI documentation
|
|
@@ -940,6 +943,7 @@ import {
|
|
|
940
943
|
formatFieldErrors,
|
|
941
944
|
useApiRequest,
|
|
942
945
|
useCancellableRequest,
|
|
946
|
+
unwrap,
|
|
943
947
|
${streamImports}
|
|
944
948
|
type ApiConfig,
|
|
945
949
|
type ParsedError,
|
|
@@ -948,7 +952,8 @@ import {
|
|
|
948
952
|
type ResponseMetadata,
|
|
949
953
|
type ApiRequestState,
|
|
950
954
|
type RequestInterceptor,
|
|
951
|
-
type ResponseInterceptor
|
|
955
|
+
type ResponseInterceptor,
|
|
956
|
+
type Unwrapped
|
|
952
957
|
} from '@bagelink/sdk';
|
|
953
958
|
import { ref, type Ref } from 'vue';
|
|
954
959
|
|
|
@@ -1102,6 +1107,7 @@ export {
|
|
|
1102
1107
|
formatFieldErrors,
|
|
1103
1108
|
useApiRequest,
|
|
1104
1109
|
useCancellableRequest,
|
|
1110
|
+
unwrap,
|
|
1105
1111
|
ApiResponse,
|
|
1106
1112
|
type ApiConfig,
|
|
1107
1113
|
type ParsedError,
|
|
@@ -1110,7 +1116,8 @@ export {
|
|
|
1110
1116
|
type FastAPIValidationError,
|
|
1111
1117
|
type FastAPIErrorResponse,
|
|
1112
1118
|
type RequestInterceptor,
|
|
1113
|
-
type ResponseInterceptor
|
|
1119
|
+
type ResponseInterceptor,
|
|
1120
|
+
type Unwrapped
|
|
1114
1121
|
}
|
|
1115
1122
|
`
|
|
1116
1123
|
|
package/src/unwrap.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unwrap utility for ApiResponse
|
|
3
|
+
*
|
|
4
|
+
* Use this when you need to assign API responses to plain types
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { ApiResponse } from './index'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Unwrap an ApiResponse to get the plain data
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* import { unwrap } from '@bagelink/sdk'
|
|
14
|
+
*
|
|
15
|
+
* const agent = ref<AgentResponse | null>(null)
|
|
16
|
+
* agent.value = unwrap(await api.agents.getByAgentId({ agentId }))
|
|
17
|
+
*/
|
|
18
|
+
export function unwrap<T>(response: ApiResponse<T> | T): T {
|
|
19
|
+
// If it's already unwrapped, return as-is
|
|
20
|
+
if (!response || typeof response !== 'object') {
|
|
21
|
+
return response as T
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// If it's an array (ApiResponse extends Array), return as-is
|
|
25
|
+
if (Array.isArray(response)) {
|
|
26
|
+
return response as unknown as T
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// If it has $raw, it's an enhanced object - return without metadata
|
|
30
|
+
if ('$raw' in response) {
|
|
31
|
+
const { $raw, $metadata, totalCount, page, perPage, totalPages, rateLimit, rateLimitReset, getHeader, hasPagination, hasNextPage, hasPrevPage, ...data } = response as any
|
|
32
|
+
return data as T
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return response as T
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Type helper to unwrap ApiResponse
|
|
40
|
+
*/
|
|
41
|
+
export type Unwrapped<T> = T extends ApiResponse<infer U> ? U : T
|