magicbell-js 0.0.3 → 0.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/package.json +2 -1
- package/src/project-client/http/client.ts +71 -0
- package/src/project-client/http/environment.ts +3 -0
- package/src/project-client/http/error.ts +14 -0
- package/src/project-client/http/errors/validation-error.ts +25 -0
- package/src/project-client/http/handlers/handler-chain.ts +30 -0
- package/src/project-client/http/handlers/hook-handler.ts +78 -0
- package/src/project-client/http/handlers/request-validation-handler.ts +111 -0
- package/src/project-client/http/handlers/response-validation-handler.ts +203 -0
- package/src/project-client/http/handlers/retry-handler.ts +60 -0
- package/src/project-client/http/handlers/terminating-handler.ts +14 -0
- package/src/project-client/http/hooks/custom-hook.ts +25 -0
- package/src/project-client/http/hooks/hook.ts +41 -0
- package/src/project-client/http/index.ts +11 -0
- package/src/project-client/http/serialization/base-serializer.ts +139 -0
- package/src/project-client/http/serialization/header-serializer.ts +19 -0
- package/src/project-client/http/serialization/path-serializer.ts +12 -0
- package/src/project-client/http/serialization/query-serializer.ts +18 -0
- package/src/project-client/http/transport/request-builder.ts +242 -0
- package/src/project-client/http/transport/request-fetch-adapter.ts +131 -0
- package/src/project-client/http/transport/request.ts +222 -0
- package/src/project-client/http/transport/transport-hook-adapter.ts +91 -0
- package/src/project-client/http/transport/types.ts +50 -0
- package/src/project-client/http/types.ts +74 -0
- package/src/project-client/http/utils/content-type.ts +33 -0
- package/src/project-client/http/utils/line-decoder.ts +35 -0
- package/src/project-client/http/utils/response-matcher.ts +25 -0
- package/src/project-client/index.ts +94 -0
- package/src/project-client/services/base-service.ts +27 -0
- package/src/project-client/services/broadcasts/broadcasts-service.ts +115 -0
- package/src/project-client/services/broadcasts/index.ts +2 -0
- package/src/project-client/services/broadcasts/models/broadcast-collection.ts +54 -0
- package/src/project-client/services/broadcasts/models/broadcast-status.ts +60 -0
- package/src/project-client/services/broadcasts/models/broadcast.ts +143 -0
- package/src/project-client/services/broadcasts/models/email.ts +57 -0
- package/src/project-client/services/broadcasts/models/errors.ts +45 -0
- package/src/project-client/services/broadcasts/models/in-app.ts +57 -0
- package/src/project-client/services/broadcasts/models/index.ts +15 -0
- package/src/project-client/services/broadcasts/models/mobile-push.ts +57 -0
- package/src/project-client/services/broadcasts/models/overrides-channels.ts +82 -0
- package/src/project-client/services/broadcasts/models/overrides.ts +59 -0
- package/src/project-client/services/broadcasts/models/providers.ts +81 -0
- package/src/project-client/services/broadcasts/models/slack.ts +57 -0
- package/src/project-client/services/broadcasts/models/sms.ts +57 -0
- package/src/project-client/services/broadcasts/models/status-status.ts +7 -0
- package/src/project-client/services/broadcasts/models/summary.ts +51 -0
- package/src/project-client/services/broadcasts/models/web-push.ts +57 -0
- package/src/project-client/services/broadcasts/request-params.ts +5 -0
- package/src/project-client/services/channels/channels-service.ts +1017 -0
- package/src/project-client/services/channels/index.ts +2 -0
- package/src/project-client/services/channels/models/apns-token-collection.ts +54 -0
- package/src/project-client/services/channels/models/apns-token-installation-id.ts +6 -0
- package/src/project-client/services/channels/models/apns-token.ts +90 -0
- package/src/project-client/services/channels/models/category-delivery-config-channels.ts +57 -0
- package/src/project-client/services/channels/models/category-delivery-config.ts +73 -0
- package/src/project-client/services/channels/models/channel.ts +11 -0
- package/src/project-client/services/channels/models/discard-result.ts +51 -0
- package/src/project-client/services/channels/models/expo-token-collection.ts +54 -0
- package/src/project-client/services/channels/models/expo-token.ts +69 -0
- package/src/project-client/services/channels/models/fcm-token-collection.ts +54 -0
- package/src/project-client/services/channels/models/fcm-token-installation-id.ts +6 -0
- package/src/project-client/services/channels/models/fcm-token.ts +75 -0
- package/src/project-client/services/channels/models/inbox-token-response-collection.ts +59 -0
- package/src/project-client/services/channels/models/inbox-token-response.ts +75 -0
- package/src/project-client/services/channels/models/index.ts +24 -0
- package/src/project-client/services/channels/models/keys.ts +51 -0
- package/src/project-client/services/channels/models/oauth.ts +57 -0
- package/src/project-client/services/channels/models/slack-token-collection.ts +54 -0
- package/src/project-client/services/channels/models/slack-token-webhook.ts +45 -0
- package/src/project-client/services/channels/models/slack-token.ts +83 -0
- package/src/project-client/services/channels/models/teams-token-collection.ts +54 -0
- package/src/project-client/services/channels/models/teams-token-webhook.ts +45 -0
- package/src/project-client/services/channels/models/teams-token.ts +76 -0
- package/src/project-client/services/channels/models/web-push-token-collection.ts +54 -0
- package/src/project-client/services/channels/models/web-push-token.ts +77 -0
- package/src/project-client/services/channels/request-params.ts +45 -0
- package/src/project-client/services/common/index.ts +1 -0
- package/src/project-client/services/common/links.ts +57 -0
- package/src/project-client/services/events/events-service.ts +84 -0
- package/src/project-client/services/events/index.ts +2 -0
- package/src/project-client/services/events/models/event-collection.ts +54 -0
- package/src/project-client/services/events/models/event.ts +81 -0
- package/src/project-client/services/events/models/index.ts +2 -0
- package/src/project-client/services/events/request-params.ts +5 -0
- package/src/project-client/services/integrations/index.ts +2 -0
- package/src/project-client/services/integrations/integrations-service.ts +1965 -0
- package/src/project-client/services/integrations/models/apns-config-collection.ts +54 -0
- package/src/project-client/services/integrations/models/apns-config-payload.ts +81 -0
- package/src/project-client/services/integrations/models/apns-config.ts +64 -0
- package/src/project-client/services/integrations/models/awssns-config-collection.ts +54 -0
- package/src/project-client/services/integrations/models/awssns-config-payload.ts +45 -0
- package/src/project-client/services/integrations/models/awssns-config.ts +64 -0
- package/src/project-client/services/integrations/models/badge.ts +6 -0
- package/src/project-client/services/integrations/models/banner.ts +63 -0
- package/src/project-client/services/integrations/models/default-hover.ts +45 -0
- package/src/project-client/services/integrations/models/default-state.ts +45 -0
- package/src/project-client/services/integrations/models/default_.ts +90 -0
- package/src/project-client/services/integrations/models/dialog.ts +57 -0
- package/src/project-client/services/integrations/models/event-source-config-collection.ts +59 -0
- package/src/project-client/services/integrations/models/event-source-config-payload.ts +45 -0
- package/src/project-client/services/integrations/models/event-source-config.ts +64 -0
- package/src/project-client/services/integrations/models/expo-config-collection.ts +54 -0
- package/src/project-client/services/integrations/models/expo-config-payload.ts +45 -0
- package/src/project-client/services/integrations/models/expo-config.ts +64 -0
- package/src/project-client/services/integrations/models/fcm-config-collection.ts +54 -0
- package/src/project-client/services/integrations/models/fcm-config-payload.ts +105 -0
- package/src/project-client/services/integrations/models/fcm-config.ts +64 -0
- package/src/project-client/services/integrations/models/footer.ts +63 -0
- package/src/project-client/services/integrations/models/github-config-collection.ts +54 -0
- package/src/project-client/services/integrations/models/github-config-payload.ts +45 -0
- package/src/project-client/services/integrations/models/github-config.ts +64 -0
- package/src/project-client/services/integrations/models/header.ts +69 -0
- package/src/project-client/services/integrations/models/icon.ts +51 -0
- package/src/project-client/services/integrations/models/images.ts +45 -0
- package/src/project-client/services/integrations/models/inbox-config-collection.ts +54 -0
- package/src/project-client/services/integrations/models/inbox-config-payload.ts +60 -0
- package/src/project-client/services/integrations/models/inbox-config.ts +64 -0
- package/src/project-client/services/integrations/models/index.ts +75 -0
- package/src/project-client/services/integrations/models/integration-config-collection.ts +59 -0
- package/src/project-client/services/integrations/models/integration-config.ts +57 -0
- package/src/project-client/services/integrations/models/mailgun-config-collection.ts +54 -0
- package/src/project-client/services/integrations/models/mailgun-config-payload-from.ts +51 -0
- package/src/project-client/services/integrations/models/mailgun-config-payload.ts +70 -0
- package/src/project-client/services/integrations/models/mailgun-config.ts +64 -0
- package/src/project-client/services/integrations/models/notification.ts +61 -0
- package/src/project-client/services/integrations/models/payload-version.ts +6 -0
- package/src/project-client/services/integrations/models/ping-config-collection.ts +54 -0
- package/src/project-client/services/integrations/models/ping-config-payload.ts +45 -0
- package/src/project-client/services/integrations/models/ping-config.ts +64 -0
- package/src/project-client/services/integrations/models/region.ts +6 -0
- package/src/project-client/services/integrations/models/reply-to.ts +51 -0
- package/src/project-client/services/integrations/models/sendgrid-config-collection.ts +54 -0
- package/src/project-client/services/integrations/models/sendgrid-config-payload-from.ts +51 -0
- package/src/project-client/services/integrations/models/sendgrid-config-payload.ts +65 -0
- package/src/project-client/services/integrations/models/sendgrid-config.ts +64 -0
- package/src/project-client/services/integrations/models/ses-config-collection.ts +54 -0
- package/src/project-client/services/integrations/models/ses-config-payload-from.ts +51 -0
- package/src/project-client/services/integrations/models/ses-config-payload.ts +70 -0
- package/src/project-client/services/integrations/models/ses-config.ts +64 -0
- package/src/project-client/services/integrations/models/slack-config-collection.ts +54 -0
- package/src/project-client/services/integrations/models/slack-config-payload.ts +63 -0
- package/src/project-client/services/integrations/models/slack-config.ts +64 -0
- package/src/project-client/services/integrations/models/stripe-config-collection.ts +54 -0
- package/src/project-client/services/integrations/models/stripe-config-payload.ts +45 -0
- package/src/project-client/services/integrations/models/stripe-config.ts +64 -0
- package/src/project-client/services/integrations/models/templates-config-collection.ts +59 -0
- package/src/project-client/services/integrations/models/templates-config.ts +57 -0
- package/src/project-client/services/integrations/models/theme.ts +89 -0
- package/src/project-client/services/integrations/models/twilio-config-collection.ts +54 -0
- package/src/project-client/services/integrations/models/twilio-config-payload.ts +75 -0
- package/src/project-client/services/integrations/models/twilio-config.ts +64 -0
- package/src/project-client/services/integrations/models/type_.ts +5 -0
- package/src/project-client/services/integrations/models/unread-hover.ts +45 -0
- package/src/project-client/services/integrations/models/unread-state.ts +45 -0
- package/src/project-client/services/integrations/models/unread.ts +66 -0
- package/src/project-client/services/integrations/models/unseen-badge.ts +45 -0
- package/src/project-client/services/integrations/models/unseen-hover.ts +45 -0
- package/src/project-client/services/integrations/models/unseen-state.ts +45 -0
- package/src/project-client/services/integrations/models/unseen.ts +66 -0
- package/src/project-client/services/integrations/models/webpush-config-collection.ts +54 -0
- package/src/project-client/services/integrations/models/webpush-config-payload.ts +51 -0
- package/src/project-client/services/integrations/models/webpush-config.ts +64 -0
- package/src/project-client/services/integrations/request-params.ts +5 -0
- package/src/project-client/services/jwt/index.ts +2 -0
- package/src/project-client/services/jwt/jwt-service.ts +233 -0
- package/src/project-client/services/jwt/models/access-token-collection.ts +54 -0
- package/src/project-client/services/jwt/models/access-token.ts +69 -0
- package/src/project-client/services/jwt/models/create-project-token-request.ts +51 -0
- package/src/project-client/services/jwt/models/create-token-response.ts +63 -0
- package/src/project-client/services/jwt/models/create-user-token-request.ts +63 -0
- package/src/project-client/services/jwt/models/discard-token-response.ts +51 -0
- package/src/project-client/services/jwt/models/index.ts +6 -0
- package/src/project-client/services/jwt/request-params.ts +11 -0
- package/src/project-client/services/notifications/index.ts +2 -0
- package/src/project-client/services/notifications/models/delivery-plan-collection.ts +54 -0
- package/src/project-client/services/notifications/models/delivery-plan.ts +93 -0
- package/src/project-client/services/notifications/models/index.ts +2 -0
- package/src/project-client/services/notifications/notifications-service.ts +44 -0
- package/src/project-client/services/users/index.ts +2 -0
- package/src/project-client/services/users/models/index.ts +2 -0
- package/src/project-client/services/users/models/user-collection.ts +54 -0
- package/src/project-client/services/users/models/user.ts +99 -0
- package/src/project-client/services/users/request-params.ts +6 -0
- package/src/project-client/services/users/users-service.ts +117 -0
- package/src/project-client.ts +1 -0
- package/src/user-client/http/client.ts +71 -0
- package/src/user-client/http/environment.ts +3 -0
- package/src/user-client/http/error.ts +14 -0
- package/src/user-client/http/errors/validation-error.ts +25 -0
- package/src/user-client/http/handlers/handler-chain.ts +30 -0
- package/src/user-client/http/handlers/hook-handler.ts +78 -0
- package/src/user-client/http/handlers/request-validation-handler.ts +111 -0
- package/src/user-client/http/handlers/response-validation-handler.ts +203 -0
- package/src/user-client/http/handlers/retry-handler.ts +60 -0
- package/src/user-client/http/handlers/terminating-handler.ts +14 -0
- package/src/user-client/http/hooks/custom-hook.ts +25 -0
- package/src/user-client/http/hooks/hook.ts +41 -0
- package/src/user-client/http/index.ts +11 -0
- package/src/user-client/http/serialization/base-serializer.ts +139 -0
- package/src/user-client/http/serialization/header-serializer.ts +19 -0
- package/src/user-client/http/serialization/path-serializer.ts +12 -0
- package/src/user-client/http/serialization/query-serializer.ts +18 -0
- package/src/user-client/http/transport/request-builder.ts +242 -0
- package/src/user-client/http/transport/request-fetch-adapter.ts +131 -0
- package/src/user-client/http/transport/request.ts +222 -0
- package/src/user-client/http/transport/transport-hook-adapter.ts +91 -0
- package/src/user-client/http/transport/types.ts +50 -0
- package/src/user-client/http/types.ts +74 -0
- package/src/user-client/http/utils/content-type.ts +33 -0
- package/src/user-client/http/utils/line-decoder.ts +35 -0
- package/src/user-client/http/utils/response-matcher.ts +25 -0
- package/src/user-client/index.ts +54 -0
- package/src/user-client/services/base-service.ts +27 -0
- package/src/user-client/services/channels/channels-service.ts +1020 -0
- package/src/user-client/services/channels/index.ts +2 -0
- package/src/user-client/services/channels/models/apns-token-collection.ts +54 -0
- package/src/user-client/services/channels/models/apns-token-installation-id.ts +6 -0
- package/src/user-client/services/channels/models/apns-token-payload-installation-id.ts +6 -0
- package/src/user-client/services/channels/models/apns-token-payload.ts +66 -0
- package/src/user-client/services/channels/models/apns-token.ts +90 -0
- package/src/user-client/services/channels/models/discard-result.ts +51 -0
- package/src/user-client/services/channels/models/expo-token-collection.ts +54 -0
- package/src/user-client/services/channels/models/expo-token-payload.ts +45 -0
- package/src/user-client/services/channels/models/expo-token.ts +69 -0
- package/src/user-client/services/channels/models/fcm-token-collection.ts +54 -0
- package/src/user-client/services/channels/models/fcm-token-installation-id.ts +6 -0
- package/src/user-client/services/channels/models/fcm-token-payload-installation-id.ts +6 -0
- package/src/user-client/services/channels/models/fcm-token-payload.ts +51 -0
- package/src/user-client/services/channels/models/fcm-token.ts +75 -0
- package/src/user-client/services/channels/models/inbox-token-response-collection.ts +59 -0
- package/src/user-client/services/channels/models/inbox-token-response.ts +75 -0
- package/src/user-client/services/channels/models/inbox-token.ts +51 -0
- package/src/user-client/services/channels/models/index.ts +33 -0
- package/src/user-client/services/channels/models/slack-token-collection.ts +54 -0
- package/src/user-client/services/channels/models/slack-token-oauth.ts +57 -0
- package/src/user-client/services/channels/models/slack-token-payload-oauth.ts +57 -0
- package/src/user-client/services/channels/models/slack-token-payload-webhook.ts +45 -0
- package/src/user-client/services/channels/models/slack-token-payload.ts +64 -0
- package/src/user-client/services/channels/models/slack-token-webhook.ts +45 -0
- package/src/user-client/services/channels/models/slack-token.ts +88 -0
- package/src/user-client/services/channels/models/teams-token-collection.ts +54 -0
- package/src/user-client/services/channels/models/teams-token-payload-webhook.ts +45 -0
- package/src/user-client/services/channels/models/teams-token-payload.ts +52 -0
- package/src/user-client/services/channels/models/teams-token-webhook.ts +45 -0
- package/src/user-client/services/channels/models/teams-token.ts +76 -0
- package/src/user-client/services/channels/models/web-push-token-collection.ts +54 -0
- package/src/user-client/services/channels/models/web-push-token-keys.ts +51 -0
- package/src/user-client/services/channels/models/web-push-token-payload-keys.ts +51 -0
- package/src/user-client/services/channels/models/web-push-token.ts +82 -0
- package/src/user-client/services/channels/request-params.ts +41 -0
- package/src/user-client/services/common/index.ts +2 -0
- package/src/user-client/services/common/links.ts +57 -0
- package/src/user-client/services/common/web-push-token-payload.ts +58 -0
- package/src/user-client/services/integrations/index.ts +2 -0
- package/src/user-client/services/integrations/integrations-service.ts +278 -0
- package/src/user-client/services/integrations/models/authed-user.ts +75 -0
- package/src/user-client/services/integrations/models/banner.ts +63 -0
- package/src/user-client/services/integrations/models/default-hover.ts +45 -0
- package/src/user-client/services/integrations/models/default-state.ts +45 -0
- package/src/user-client/services/integrations/models/default_.ts +90 -0
- package/src/user-client/services/integrations/models/dialog.ts +57 -0
- package/src/user-client/services/integrations/models/enterprise.ts +51 -0
- package/src/user-client/services/integrations/models/footer.ts +63 -0
- package/src/user-client/services/integrations/models/header.ts +69 -0
- package/src/user-client/services/integrations/models/icon.ts +51 -0
- package/src/user-client/services/integrations/models/images.ts +45 -0
- package/src/user-client/services/integrations/models/inbox-config-payload.ts +60 -0
- package/src/user-client/services/integrations/models/incoming-webhook.ts +57 -0
- package/src/user-client/services/integrations/models/index.ts +29 -0
- package/src/user-client/services/integrations/models/slack-finish-install-response.ts +57 -0
- package/src/user-client/services/integrations/models/slack-installation.ts +136 -0
- package/src/user-client/services/integrations/models/slack-start-install-response-content.ts +57 -0
- package/src/user-client/services/integrations/models/slack-start-install.ts +63 -0
- package/src/user-client/services/integrations/models/team.ts +51 -0
- package/src/user-client/services/integrations/models/templates-installation.ts +57 -0
- package/src/user-client/services/integrations/models/theme-notification.ts +61 -0
- package/src/user-client/services/integrations/models/theme.ts +94 -0
- package/src/user-client/services/integrations/models/unread-hover.ts +45 -0
- package/src/user-client/services/integrations/models/unread-state.ts +45 -0
- package/src/user-client/services/integrations/models/unread.ts +66 -0
- package/src/user-client/services/integrations/models/unseen-badge.ts +45 -0
- package/src/user-client/services/integrations/models/unseen-hover.ts +45 -0
- package/src/user-client/services/integrations/models/unseen-state.ts +45 -0
- package/src/user-client/services/integrations/models/unseen.ts +66 -0
- package/src/user-client/services/integrations/models/web-push-start-installation-response.ts +51 -0
- package/src/user-client/services/notifications/index.ts +2 -0
- package/src/user-client/services/notifications/models/index.ts +2 -0
- package/src/user-client/services/notifications/models/notification-collection.ts +54 -0
- package/src/user-client/services/notifications/models/notification.ts +129 -0
- package/src/user-client/services/notifications/notifications-service.ts +189 -0
- package/src/user-client/services/notifications/request-params.ts +7 -0
- package/src/user-client.ts +1 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { HttpError } from '../error.js';
|
|
2
|
+
import { HttpMetadata, HttpMethod, HttpResponse } from '../types.js';
|
|
3
|
+
import { LineDecoder } from '../utils/line-decoder.js';
|
|
4
|
+
import { Request } from './request.js';
|
|
5
|
+
|
|
6
|
+
interface HttpAdapter {
|
|
7
|
+
send(): Promise<HttpResponse>;
|
|
8
|
+
stream(): AsyncGenerator<HttpResponse>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class RequestFetchAdapter<T> implements HttpAdapter {
|
|
12
|
+
private requestInit: RequestInit = {};
|
|
13
|
+
|
|
14
|
+
constructor(private request: Request) {
|
|
15
|
+
this.setMethod(request.method);
|
|
16
|
+
this.setHeaders(request.getHeaders());
|
|
17
|
+
this.setBody(request.body);
|
|
18
|
+
this.setTimeout(request.config.timeoutMs);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public async send(): Promise<HttpResponse<T>> {
|
|
22
|
+
const response = await fetch(this.request.constructFullUrl(), this.requestInit);
|
|
23
|
+
|
|
24
|
+
const metadata: HttpMetadata = {
|
|
25
|
+
status: response.status,
|
|
26
|
+
statusText: response.statusText || '',
|
|
27
|
+
headers: this.getHeaders(response),
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
metadata,
|
|
32
|
+
raw: await response.clone().arrayBuffer(),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public async *stream(): AsyncGenerator<HttpResponse<T>> {
|
|
37
|
+
const response = await fetch(this.request.constructFullUrl(), this.requestInit);
|
|
38
|
+
|
|
39
|
+
const metadata: HttpMetadata = {
|
|
40
|
+
status: response.status,
|
|
41
|
+
statusText: response.statusText || '',
|
|
42
|
+
headers: this.getHeaders(response),
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
if (response.status >= 400) {
|
|
46
|
+
throw new HttpError(metadata, await response.clone().arrayBuffer());
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (!response.body) {
|
|
50
|
+
return yield {
|
|
51
|
+
metadata,
|
|
52
|
+
raw: await response.clone().arrayBuffer(),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const reader = response.body.getReader();
|
|
57
|
+
const lineDecoder = new LineDecoder();
|
|
58
|
+
|
|
59
|
+
while (true) {
|
|
60
|
+
const { done, value } = await reader.read();
|
|
61
|
+
if (done) {
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
for (const line of lineDecoder.splitLines(value)) {
|
|
66
|
+
yield {
|
|
67
|
+
metadata,
|
|
68
|
+
raw: line,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
for (const line of lineDecoder.flush()) {
|
|
74
|
+
yield {
|
|
75
|
+
metadata,
|
|
76
|
+
raw: line,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
private setMethod(method: HttpMethod): void {
|
|
82
|
+
if (!method) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
this.requestInit = {
|
|
86
|
+
...this.requestInit,
|
|
87
|
+
method,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private setBody(body: ReadableStream<Uint8Array> | null): void {
|
|
92
|
+
if (!body) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
this.requestInit = {
|
|
96
|
+
...this.requestInit,
|
|
97
|
+
body,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private setHeaders(headers: HeadersInit | undefined): void {
|
|
102
|
+
if (!headers) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
this.requestInit = {
|
|
107
|
+
...this.requestInit,
|
|
108
|
+
headers,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private setTimeout(timeoutMs: number | undefined): void {
|
|
113
|
+
if (!timeoutMs) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
this.requestInit = {
|
|
118
|
+
...this.requestInit,
|
|
119
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private getHeaders(response: Response): Record<string, string> {
|
|
124
|
+
const headers: Record<string, string> = {};
|
|
125
|
+
response.headers.forEach((value: string, key: string) => {
|
|
126
|
+
headers[key] = value;
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
return headers;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { ZodType } from 'zod';
|
|
2
|
+
|
|
3
|
+
import { HttpRequest } from '../hooks/hook.js';
|
|
4
|
+
import { SerializationStyle } from '../serialization/base-serializer.js';
|
|
5
|
+
import { HeaderSerializer } from '../serialization/header-serializer.js';
|
|
6
|
+
import { PathSerializer } from '../serialization/path-serializer.js';
|
|
7
|
+
import { QuerySerializer } from '../serialization/query-serializer.js';
|
|
8
|
+
import { ContentType, HttpMethod, RetryOptions, SdkConfig, ValidationOptions } from '../types.js';
|
|
9
|
+
import {
|
|
10
|
+
CreateRequestParameters,
|
|
11
|
+
ErrorDefinition,
|
|
12
|
+
RequestPagination,
|
|
13
|
+
RequestParameter,
|
|
14
|
+
ResponseDefinition,
|
|
15
|
+
} from './types.js';
|
|
16
|
+
|
|
17
|
+
export class Request<PageSchema = unknown[]> {
|
|
18
|
+
public baseUrl = '';
|
|
19
|
+
|
|
20
|
+
public headers: Map<string, RequestParameter> = new Map();
|
|
21
|
+
|
|
22
|
+
public queryParams: Map<string, RequestParameter> = new Map();
|
|
23
|
+
|
|
24
|
+
public pathParams: Map<string, RequestParameter> = new Map();
|
|
25
|
+
|
|
26
|
+
public body?: any;
|
|
27
|
+
|
|
28
|
+
public method: HttpMethod;
|
|
29
|
+
|
|
30
|
+
public path: string;
|
|
31
|
+
|
|
32
|
+
public config: SdkConfig;
|
|
33
|
+
|
|
34
|
+
public responses: ResponseDefinition[];
|
|
35
|
+
|
|
36
|
+
public errors: ErrorDefinition[];
|
|
37
|
+
|
|
38
|
+
public requestSchema: ZodType;
|
|
39
|
+
|
|
40
|
+
public requestContentType: ContentType;
|
|
41
|
+
|
|
42
|
+
public validation: ValidationOptions = {} as any;
|
|
43
|
+
|
|
44
|
+
public retry: RetryOptions = {} as any;
|
|
45
|
+
|
|
46
|
+
public pagination?: RequestPagination<PageSchema>;
|
|
47
|
+
|
|
48
|
+
private readonly pathPattern: string;
|
|
49
|
+
|
|
50
|
+
constructor(params: CreateRequestParameters<PageSchema>) {
|
|
51
|
+
this.baseUrl = params.baseUrl;
|
|
52
|
+
this.method = params.method;
|
|
53
|
+
this.pathPattern = params.path;
|
|
54
|
+
this.body = params.body;
|
|
55
|
+
this.path = this.constructPath();
|
|
56
|
+
this.config = params.config;
|
|
57
|
+
this.pathParams = params.pathParams;
|
|
58
|
+
this.headers = params.headers;
|
|
59
|
+
this.queryParams = params.queryParams;
|
|
60
|
+
this.responses = params.responses;
|
|
61
|
+
this.errors = params.errors;
|
|
62
|
+
this.requestSchema = params.requestSchema;
|
|
63
|
+
this.requestContentType = params.requestContentType;
|
|
64
|
+
this.retry = params.retry;
|
|
65
|
+
this.validation = params.validation;
|
|
66
|
+
this.pagination = params.pagination;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
addHeaderParam(key: string, param: RequestParameter): void {
|
|
70
|
+
if (param.value === undefined) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (param.explode === undefined) {
|
|
75
|
+
param.explode = false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (param.style === undefined) {
|
|
79
|
+
param.style = SerializationStyle.SIMPLE;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (param.encode === undefined) {
|
|
83
|
+
param.encode = false;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
this.headers.set(key, param);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
addQueryParam(key: string, param: RequestParameter): void {
|
|
90
|
+
if (param.value === undefined) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (param.explode === undefined) {
|
|
95
|
+
param.explode = true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (param.style === undefined) {
|
|
99
|
+
param.style = SerializationStyle.FORM;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (param.encode === undefined) {
|
|
103
|
+
param.encode = true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
this.queryParams.set(key, param);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
addPathParam(key: string, param: RequestParameter): void {
|
|
110
|
+
if (param.value === undefined) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (param.explode === undefined) {
|
|
115
|
+
param.explode = false;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (param.style === undefined) {
|
|
119
|
+
param.style = SerializationStyle.SIMPLE;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (param.encode === undefined) {
|
|
123
|
+
param.encode = true;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
this.pathParams.set(key, param);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
addBody(body: any): void {
|
|
130
|
+
if (body === undefined) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
this.body = body;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
public updateFromHookRequest(hookRequest: HttpRequest): void {
|
|
138
|
+
this.baseUrl = hookRequest.baseUrl;
|
|
139
|
+
this.method = hookRequest.method;
|
|
140
|
+
this.path = hookRequest.path;
|
|
141
|
+
this.body = hookRequest.body;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
public constructFullUrl(): string {
|
|
145
|
+
const queryString = new QuerySerializer().serialize(this.queryParams);
|
|
146
|
+
const path = this.constructPath();
|
|
147
|
+
const baseUrl = this.baseUrl;
|
|
148
|
+
|
|
149
|
+
return `${baseUrl}${path}${queryString}`;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public copy(overrides?: Partial<CreateRequestParameters>) {
|
|
153
|
+
const createRequestParams: CreateRequestParameters = {
|
|
154
|
+
baseUrl: overrides?.baseUrl ?? this.baseUrl,
|
|
155
|
+
errors: overrides?.errors ?? this.errors,
|
|
156
|
+
method: overrides?.method ?? this.method,
|
|
157
|
+
path: overrides?.path ?? this.path,
|
|
158
|
+
body: overrides?.body ?? this.body,
|
|
159
|
+
config: overrides?.config ?? this.config,
|
|
160
|
+
pathParams: overrides?.pathParams ?? this.pathParams,
|
|
161
|
+
queryParams: overrides?.queryParams ?? this.queryParams,
|
|
162
|
+
headers: overrides?.headers ?? this.headers,
|
|
163
|
+
responses: overrides?.responses ?? this.responses,
|
|
164
|
+
requestSchema: overrides?.requestSchema ?? this.requestSchema,
|
|
165
|
+
requestContentType: overrides?.requestContentType ?? this.requestContentType,
|
|
166
|
+
retry: overrides?.retry ?? this.retry,
|
|
167
|
+
validation: overrides?.validation ?? this.validation,
|
|
168
|
+
};
|
|
169
|
+
return new Request({
|
|
170
|
+
...createRequestParams,
|
|
171
|
+
...overrides,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
public getHeaders(): HeadersInit | undefined {
|
|
176
|
+
if (!this.headers || !this.headers.size) {
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return new HeaderSerializer().serialize(this.headers);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
public nextPage(): void {
|
|
184
|
+
if (!this.pagination) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const offsetParam = this.getOffsetParam();
|
|
189
|
+
if (!offsetParam) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
offsetParam.value = Number(offsetParam.value) + this.pagination.pageSize;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
private constructPath(): string {
|
|
197
|
+
return new PathSerializer().serialize(this.pathPattern, this.pathParams);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private getOffsetParam(): RequestParameter | undefined {
|
|
201
|
+
const offsetParam = this.getAllParams().find((param) => param.isOffset);
|
|
202
|
+
return offsetParam;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
private getAllParams(): RequestParameter[] {
|
|
206
|
+
const allParams: RequestParameter[] = [];
|
|
207
|
+
|
|
208
|
+
this.headers.forEach((val, _) => {
|
|
209
|
+
allParams.push(val);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
this.queryParams.forEach((val, _) => {
|
|
213
|
+
allParams.push(val);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
this.pathParams.forEach((val, _) => {
|
|
217
|
+
allParams.push(val);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
return allParams;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { CustomHook } from '../hooks/custom-hook.js';
|
|
2
|
+
import { HttpError, HttpRequest } from '../hooks/hook.js';
|
|
3
|
+
import { SerializationStyle } from '../serialization/base-serializer.js';
|
|
4
|
+
import { HttpResponse } from '../types.js';
|
|
5
|
+
import { Request } from './request.js';
|
|
6
|
+
import { RequestParameter } from './types.js';
|
|
7
|
+
|
|
8
|
+
export class TransportHookAdapter<T> {
|
|
9
|
+
private hook: CustomHook = new CustomHook();
|
|
10
|
+
|
|
11
|
+
public async beforeRequest(request: Request, params: Map<string, string>): Promise<Request> {
|
|
12
|
+
const hookRequest = this.requestToHookRequest(request);
|
|
13
|
+
|
|
14
|
+
const newRequest = await this.hook.beforeRequest(hookRequest, params);
|
|
15
|
+
|
|
16
|
+
const newTransportRequest = request.copy({
|
|
17
|
+
baseUrl: newRequest.baseUrl,
|
|
18
|
+
method: newRequest.method,
|
|
19
|
+
path: newRequest.path,
|
|
20
|
+
body: newRequest.body,
|
|
21
|
+
queryParams: this.hookParamsToTransportParams(newRequest.queryParams, request.queryParams, true),
|
|
22
|
+
headers: this.hookParamsToTransportParams(newRequest.headers, request.headers, false),
|
|
23
|
+
pathParams: this.hookParamsToTransportParams(newRequest.pathParams, request.headers, false),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return newTransportRequest;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public async afterResponse(
|
|
30
|
+
request: Request,
|
|
31
|
+
response: HttpResponse<T>,
|
|
32
|
+
params: Map<string, string>,
|
|
33
|
+
): Promise<HttpResponse<T>> {
|
|
34
|
+
const hookRequest = this.requestToHookRequest(request);
|
|
35
|
+
return this.hook.afterResponse(hookRequest, response, params);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public async onError(request: Request, response: HttpResponse<T>, params: Map<string, string>): Promise<HttpError> {
|
|
39
|
+
const hookRequest = this.requestToHookRequest(request);
|
|
40
|
+
return this.hook.onError(hookRequest, response, params);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private requestToHookRequest(request: Request): HttpRequest {
|
|
44
|
+
const hookHeaders: Map<string, unknown> = new Map();
|
|
45
|
+
request.headers.forEach((header, key) => {
|
|
46
|
+
hookHeaders.set(key, header.value);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const hookQueryParams: Map<string, unknown> = new Map();
|
|
50
|
+
request.queryParams.forEach((queryParam, key) => {
|
|
51
|
+
hookQueryParams.set(key, queryParam.value);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const hookPathParams: Map<string, unknown> = new Map();
|
|
55
|
+
request.pathParams.forEach((pathParam, key) => {
|
|
56
|
+
hookPathParams.set(key, pathParam.value);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const hookRequest: HttpRequest = {
|
|
60
|
+
baseUrl: request.baseUrl,
|
|
61
|
+
method: request.method,
|
|
62
|
+
path: request.path,
|
|
63
|
+
headers: hookHeaders,
|
|
64
|
+
body: request.body,
|
|
65
|
+
queryParams: hookQueryParams,
|
|
66
|
+
pathParams: hookPathParams,
|
|
67
|
+
};
|
|
68
|
+
return hookRequest;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private hookParamsToTransportParams<T>(
|
|
72
|
+
hookParams: Map<string, unknown>,
|
|
73
|
+
originalTransportParams: Map<string, RequestParameter>,
|
|
74
|
+
encode: boolean,
|
|
75
|
+
): Map<string, RequestParameter> {
|
|
76
|
+
const transportParams: Map<string, RequestParameter> = new Map();
|
|
77
|
+
hookParams.forEach((hookParamValue, hookParamKey) => {
|
|
78
|
+
const requestParam = originalTransportParams.get(hookParamKey);
|
|
79
|
+
transportParams.set(hookParamKey, {
|
|
80
|
+
key: hookParamKey,
|
|
81
|
+
value: hookParamValue,
|
|
82
|
+
encode: requestParam?.encode ?? false,
|
|
83
|
+
style: requestParam?.style || SerializationStyle.NONE,
|
|
84
|
+
explode: requestParam?.explode ?? false,
|
|
85
|
+
isLimit: requestParam?.isLimit ?? false,
|
|
86
|
+
isOffset: requestParam?.isOffset ?? false,
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
return transportParams;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ZodType } from 'zod';
|
|
2
|
+
|
|
3
|
+
import { SerializationStyle } from '../serialization/base-serializer.js';
|
|
4
|
+
import { ContentType, HttpMethod, RetryOptions, SdkConfig, ValidationOptions } from '../types.js';
|
|
5
|
+
|
|
6
|
+
export interface ResponseDefinition {
|
|
7
|
+
schema: ZodType;
|
|
8
|
+
contentType: ContentType;
|
|
9
|
+
status: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ErrorDefinition {
|
|
13
|
+
error: new (...args: any[]) => Error;
|
|
14
|
+
contentType: ContentType;
|
|
15
|
+
status: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface CreateRequestParameters<Page = unknown[]> {
|
|
19
|
+
baseUrl: string;
|
|
20
|
+
method: HttpMethod;
|
|
21
|
+
body?: any;
|
|
22
|
+
headers: Map<string, RequestParameter>;
|
|
23
|
+
queryParams: Map<string, RequestParameter>;
|
|
24
|
+
pathParams: Map<string, RequestParameter>;
|
|
25
|
+
path: string;
|
|
26
|
+
config: SdkConfig;
|
|
27
|
+
responses: ResponseDefinition[];
|
|
28
|
+
errors: ErrorDefinition[];
|
|
29
|
+
requestSchema: ZodType;
|
|
30
|
+
requestContentType: ContentType;
|
|
31
|
+
validation: ValidationOptions;
|
|
32
|
+
retry: RetryOptions;
|
|
33
|
+
pagination?: RequestPagination<Page>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface RequestParameter {
|
|
37
|
+
key: string | undefined;
|
|
38
|
+
value: unknown;
|
|
39
|
+
explode: boolean;
|
|
40
|
+
encode: boolean;
|
|
41
|
+
style: SerializationStyle;
|
|
42
|
+
isLimit: boolean;
|
|
43
|
+
isOffset: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface RequestPagination<Page> {
|
|
47
|
+
pageSize: number;
|
|
48
|
+
pagePath: string[];
|
|
49
|
+
pageSchema?: ZodType<Page, any, any>;
|
|
50
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { ZodType } from 'zod';
|
|
2
|
+
|
|
3
|
+
import { Environment } from './environment.js';
|
|
4
|
+
import { Request } from './transport/request.js';
|
|
5
|
+
|
|
6
|
+
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
|
|
7
|
+
|
|
8
|
+
export interface SdkConfig {
|
|
9
|
+
baseUrl?: string;
|
|
10
|
+
environment?: Environment;
|
|
11
|
+
timeoutMs?: number;
|
|
12
|
+
token?: string;
|
|
13
|
+
retry?: RetryOptions;
|
|
14
|
+
validation?: ValidationOptions;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface HttpMetadata {
|
|
18
|
+
status: number;
|
|
19
|
+
statusText: string;
|
|
20
|
+
headers: Record<string, string>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface HttpResponse<T = unknown> {
|
|
24
|
+
data?: T;
|
|
25
|
+
metadata: HttpMetadata;
|
|
26
|
+
raw: ArrayBuffer;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface RequestHandler {
|
|
30
|
+
next?: RequestHandler;
|
|
31
|
+
|
|
32
|
+
handle<T>(request: Request): Promise<HttpResponse<T>>;
|
|
33
|
+
stream<T>(request: Request): AsyncGenerator<HttpResponse<T>>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export enum ContentType {
|
|
37
|
+
Json = 'json',
|
|
38
|
+
Xml = 'xml',
|
|
39
|
+
Pdf = 'pdf',
|
|
40
|
+
Image = 'image',
|
|
41
|
+
File = 'file',
|
|
42
|
+
Binary = 'binary',
|
|
43
|
+
FormUrlEncoded = 'form',
|
|
44
|
+
Text = 'text',
|
|
45
|
+
MultipartFormData = 'multipartFormData',
|
|
46
|
+
EventStream = 'eventStream',
|
|
47
|
+
NoContent = 'noContent',
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface Options<T> {
|
|
51
|
+
responseSchema: ZodType<T, any, any>;
|
|
52
|
+
requestSchema?: ZodType;
|
|
53
|
+
body?: any;
|
|
54
|
+
requestContentType?: ContentType;
|
|
55
|
+
responseContentType?: ContentType;
|
|
56
|
+
abortSignal?: AbortSignal;
|
|
57
|
+
queryParams?: Record<string, unknown>;
|
|
58
|
+
retry?: RetryOptions;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface RequestConfig {
|
|
62
|
+
retry?: RetryOptions;
|
|
63
|
+
validation?: ValidationOptions;
|
|
64
|
+
baseUrl?: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface RetryOptions {
|
|
68
|
+
attempts: number;
|
|
69
|
+
delayMs?: number;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface ValidationOptions {
|
|
73
|
+
responseValidation?: boolean;
|
|
74
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ContentType } from '../types.js';
|
|
2
|
+
|
|
3
|
+
export function getContentTypeDefinition(contentType: string): ContentType {
|
|
4
|
+
if (contentType.startsWith('application/') && contentType.includes('xml')) {
|
|
5
|
+
return ContentType.Xml;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (contentType.toLowerCase() === 'application/x-www-form-urlencoded') {
|
|
9
|
+
return ContentType.FormUrlEncoded;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (contentType.toLowerCase() === 'text/event-stream') {
|
|
13
|
+
return ContentType.EventStream;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (contentType.toLowerCase().startsWith('text/')) {
|
|
17
|
+
return ContentType.Text;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (contentType.toLowerCase().startsWith('image/')) {
|
|
21
|
+
return ContentType.Image;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (contentType.toLowerCase() === 'application/octet-stream') {
|
|
25
|
+
return ContentType.Binary;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (contentType.toLowerCase() === 'application/json') {
|
|
29
|
+
return ContentType.Json;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return ContentType.Json;
|
|
33
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export class LineDecoder {
|
|
2
|
+
private lineBuffer = '';
|
|
3
|
+
private decoder = new TextDecoder();
|
|
4
|
+
private encoder = new TextEncoder();
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Splits the given chunk into lines.
|
|
8
|
+
* Stores incomplete lines in a buffer and returns them when the next chunk arrives.
|
|
9
|
+
*/
|
|
10
|
+
public splitLines(chunk: Uint8Array): Uint8Array[] {
|
|
11
|
+
this.lineBuffer += this.decoder.decode(chunk);
|
|
12
|
+
|
|
13
|
+
let lineEndIndex;
|
|
14
|
+
const lines: Uint8Array[] = [];
|
|
15
|
+
while ((lineEndIndex = this.lineBuffer.indexOf('\n')) >= 0) {
|
|
16
|
+
const line = this.lineBuffer.slice(0, lineEndIndex + 1); // Include the newline character
|
|
17
|
+
this.lineBuffer = this.lineBuffer.slice(lineEndIndex + 1);
|
|
18
|
+
if (line.length > 1) {
|
|
19
|
+
lines.push(this.encoder.encode(line));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return lines;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Returns the remaining lines in the buffer. */
|
|
27
|
+
public flush(): Uint8Array[] {
|
|
28
|
+
if (this.lineBuffer.length === 0) {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
const lines = [this.encoder.encode(this.lineBuffer)];
|
|
32
|
+
this.lineBuffer = '';
|
|
33
|
+
return lines;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ResponseDefinition } from '../transport/types.js';
|
|
2
|
+
import { ContentType, HttpResponse } from '../types.js';
|
|
3
|
+
import { getContentTypeDefinition } from './content-type.js';
|
|
4
|
+
|
|
5
|
+
export class ResponseMatcher {
|
|
6
|
+
constructor(private responses: ResponseDefinition[]) {}
|
|
7
|
+
|
|
8
|
+
public getResponseDefinition(response: HttpResponse): ResponseDefinition | undefined {
|
|
9
|
+
const rawContentType = response.metadata.headers['content-type']?.toLocaleLowerCase() || '';
|
|
10
|
+
const contentType = getContentTypeDefinition(rawContentType);
|
|
11
|
+
const statusCode = response.metadata.status;
|
|
12
|
+
|
|
13
|
+
if (!this.responses.length) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (this.responses.length === 1) {
|
|
18
|
+
return this.responses[0];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return this.responses.find((response) => {
|
|
22
|
+
return response.contentType === contentType && response.status === statusCode;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|