@executor-js/sdk 0.0.2 → 0.1.0

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.
@@ -1033,74 +1033,118 @@ var discoverAuthorizationServerMetadata = (issuer, options = {}) => Effect7.gen(
1033
1033
  }
1034
1034
  return null;
1035
1035
  });
1036
- var asForDcr = (registrationEndpoint) => {
1037
- const url = new URL(registrationEndpoint);
1038
- return {
1039
- issuer: `${url.protocol}//${url.host}`,
1040
- registration_endpoint: registrationEndpoint
1041
- };
1036
+ var DcrErrorBody = class extends Data4.TaggedError("DcrErrorBody") {
1042
1037
  };
1043
- var registerDynamicClient = (input, options = {}) => Effect7.tryPromise({
1044
- try: async () => {
1045
- const as = asForDcr(input.registrationEndpoint);
1046
- const m = input.metadata;
1047
- const clientMetadata = {
1048
- redirect_uris: [...m.redirect_uris]
1049
- };
1050
- if (m.client_name !== void 0) clientMetadata.client_name = m.client_name;
1051
- if (m.grant_types !== void 0) clientMetadata.grant_types = [...m.grant_types];
1052
- if (m.response_types !== void 0) clientMetadata.response_types = [...m.response_types];
1053
- if (m.token_endpoint_auth_method !== void 0) {
1054
- clientMetadata.token_endpoint_auth_method = m.token_endpoint_auth_method;
1055
- }
1056
- if (m.scope !== void 0) clientMetadata.scope = m.scope;
1057
- if (m.application_type !== void 0) clientMetadata.application_type = m.application_type;
1058
- if (m.client_uri !== void 0) clientMetadata.client_uri = m.client_uri;
1059
- if (m.logo_uri !== void 0) clientMetadata.logo_uri = m.logo_uri;
1060
- if (m.contacts !== void 0) clientMetadata.contacts = [...m.contacts];
1061
- if (m.software_id !== void 0) clientMetadata.software_id = m.software_id;
1062
- if (m.software_version !== void 0) clientMetadata.software_version = m.software_version;
1063
- if (m.extra) for (const [k, v] of Object.entries(m.extra)) clientMetadata[k] = v;
1064
- const reqOptions = oauth4webapiOptions(options);
1065
- if (isLoopbackHttpUrl2(input.registrationEndpoint)) {
1066
- reqOptions[oauth2.allowInsecureRequests] = true;
1067
- }
1068
- if (input.initialAccessToken) {
1069
- reqOptions.initialAccessToken = input.initialAccessToken;
1070
- }
1071
- const response = await oauth2.dynamicClientRegistrationRequest(
1072
- as,
1073
- clientMetadata,
1074
- reqOptions
1075
- );
1076
- const client = await oauth2.processDynamicClientRegistrationResponse(
1077
- response
1078
- );
1079
- return client;
1080
- },
1081
- catch: (cause) => {
1082
- if (cause instanceof oauth2.ResponseBodyError) {
1083
- return discoveryError(
1084
- `Dynamic Client Registration failed: ${cause.error}${cause.error_description ? ` \u2014 ${cause.error_description}` : ""}`,
1085
- { status: cause.status, cause }
1086
- );
1038
+ var DcrTransport = class extends Data4.TaggedError("DcrTransport") {
1039
+ };
1040
+ var buildDcrBody = (m) => {
1041
+ const body = { redirect_uris: [...m.redirect_uris] };
1042
+ if (m.client_name !== void 0) body.client_name = m.client_name;
1043
+ if (m.grant_types !== void 0) body.grant_types = [...m.grant_types];
1044
+ if (m.response_types !== void 0) body.response_types = [...m.response_types];
1045
+ if (m.token_endpoint_auth_method !== void 0) {
1046
+ body.token_endpoint_auth_method = m.token_endpoint_auth_method;
1047
+ }
1048
+ if (m.scope !== void 0) body.scope = m.scope;
1049
+ if (m.application_type !== void 0) body.application_type = m.application_type;
1050
+ if (m.client_uri !== void 0) body.client_uri = m.client_uri;
1051
+ if (m.logo_uri !== void 0) body.logo_uri = m.logo_uri;
1052
+ if (m.contacts !== void 0) body.contacts = [...m.contacts];
1053
+ if (m.software_id !== void 0) body.software_id = m.software_id;
1054
+ if (m.software_version !== void 0) body.software_version = m.software_version;
1055
+ if (m.extra) for (const [k, v] of Object.entries(m.extra)) body[k] = v;
1056
+ return body;
1057
+ };
1058
+ var interpretDcrFailure = (status, text) => {
1059
+ if (status >= 400 && status < 500) {
1060
+ const parsed = Result.try({
1061
+ try: () => text ? JSON.parse(text) : null,
1062
+ catch: () => null
1063
+ });
1064
+ const body = Result.isSuccess(parsed) ? parsed.success : null;
1065
+ if (body && typeof body === "object" && "error" in body && typeof body.error === "string" && body.error.length > 0) {
1066
+ const desc = "error_description" in body && typeof body.error_description === "string" ? body.error_description : void 0;
1067
+ return new DcrErrorBody({ status, error: body.error, error_description: desc });
1087
1068
  }
1088
- return discoveryError(
1089
- `Dynamic Client Registration failed: ${cause instanceof Error ? cause.message : String(cause)}`,
1090
- { cause }
1069
+ }
1070
+ return new DcrTransport({
1071
+ message: `Dynamic Client Registration endpoint returned status ${status}${text ? ` \u2014 ${text.slice(0, 200)}` : ""}`,
1072
+ status
1073
+ });
1074
+ };
1075
+ var registerDynamicClient = (input, options = {}) => Effect7.gen(function* () {
1076
+ const url = new URL(input.registrationEndpoint);
1077
+ if (url.protocol !== "https:" && !isLoopbackHttpUrl2(input.registrationEndpoint)) {
1078
+ return yield* new DcrTransport({
1079
+ message: `registration_endpoint must be HTTPS or a loopback HTTP URL (got ${url.protocol}//${url.host})`
1080
+ });
1081
+ }
1082
+ const headers = {
1083
+ "content-type": "application/json",
1084
+ accept: "application/json"
1085
+ };
1086
+ if (input.initialAccessToken) {
1087
+ headers.authorization = `Bearer ${input.initialAccessToken}`;
1088
+ }
1089
+ const fetchImpl = options.fetch ?? globalThis.fetch;
1090
+ const timeoutMs = options.timeoutMs ?? OAUTH2_DEFAULT_TIMEOUT_MS;
1091
+ const response = yield* Effect7.tryPromise({
1092
+ try: () => fetchImpl(input.registrationEndpoint, {
1093
+ method: "POST",
1094
+ headers,
1095
+ body: JSON.stringify(buildDcrBody(input.metadata)),
1096
+ signal: AbortSignal.timeout(timeoutMs)
1097
+ }),
1098
+ catch: (cause) => new DcrTransport({
1099
+ message: `Dynamic Client Registration request failed: ${cause instanceof Error ? cause.message : String(cause)}`,
1100
+ cause
1101
+ })
1102
+ });
1103
+ if (response.status !== 200 && response.status !== 201) {
1104
+ const text2 = yield* Effect7.promise(
1105
+ () => response.text().catch(() => "")
1091
1106
  );
1107
+ return yield* interpretDcrFailure(response.status, text2);
1092
1108
  }
1109
+ const text = yield* Effect7.tryPromise({
1110
+ try: () => response.text(),
1111
+ catch: (cause) => new DcrTransport({
1112
+ message: "Dynamic Client Registration response could not be read",
1113
+ status: response.status,
1114
+ cause
1115
+ })
1116
+ });
1117
+ const json = yield* Effect7.try({
1118
+ try: () => JSON.parse(text),
1119
+ catch: (cause) => new DcrTransport({
1120
+ message: "Dynamic Client Registration response was not valid JSON",
1121
+ status: response.status,
1122
+ cause
1123
+ })
1124
+ });
1125
+ return yield* decodeClientInformation(json).pipe(
1126
+ Effect7.mapError(
1127
+ (err) => new OAuthDiscoveryError({
1128
+ message: `Dynamic Client Registration response is malformed: ${Schema7.isSchemaError(err) ? err.message : String(err)}`,
1129
+ cause: err
1130
+ })
1131
+ )
1132
+ );
1093
1133
  }).pipe(
1094
- Effect7.flatMap(
1095
- (raw) => decodeClientInformation(raw).pipe(
1096
- Effect7.mapError(
1097
- (err) => new OAuthDiscoveryError({
1098
- message: `Dynamic Client Registration response is malformed: ${Schema7.isSchemaError(err) ? err.message : String(err)}`,
1099
- cause: err
1100
- })
1134
+ Effect7.catchTags({
1135
+ DcrErrorBody: (err) => Effect7.fail(
1136
+ discoveryError(
1137
+ `Dynamic Client Registration failed: ${err.error}${err.error_description ? ` \u2014 ${err.error_description}` : ""}`,
1138
+ { status: err.status, cause: err }
1101
1139
  )
1140
+ ),
1141
+ DcrTransport: (err) => Effect7.fail(
1142
+ discoveryError(`Dynamic Client Registration failed: ${err.message}`, {
1143
+ status: err.status,
1144
+ cause: err.cause ?? err
1145
+ })
1102
1146
  )
1103
- )
1147
+ })
1104
1148
  );
1105
1149
  var beginDynamicAuthorization = (input, options = {}) => Effect7.gen(function* () {
1106
1150
  const prior = input.previousState ?? {};
@@ -4393,4 +4437,4 @@ export {
4393
4437
  resolveSecretBackedMap,
4394
4438
  defineExecutorConfig
4395
4439
  };
4396
- //# sourceMappingURL=chunk-6LMMN2GP.js.map
4440
+ //# sourceMappingURL=chunk-2WV7VSNL.js.map