@steamsets/client-ts 0.1.2

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.
Files changed (228) hide show
  1. package/.devcontainer/README.md +35 -0
  2. package/README.md +389 -0
  3. package/core.d.ts +10 -0
  4. package/core.d.ts.map +1 -0
  5. package/core.js +17 -0
  6. package/core.js.map +1 -0
  7. package/docs/sdks/account/README.md +97 -0
  8. package/docs/sdks/liveness/README.md +51 -0
  9. package/docs/sdks/steamsets/README.md +7 -0
  10. package/funcs/accountAccountV1Apps.d.ts +13 -0
  11. package/funcs/accountAccountV1Apps.d.ts.map +1 -0
  12. package/funcs/accountAccountV1Apps.js +82 -0
  13. package/funcs/accountAccountV1Apps.js.map +1 -0
  14. package/funcs/accountAccountV1Badge.d.ts +13 -0
  15. package/funcs/accountAccountV1Badge.d.ts.map +1 -0
  16. package/funcs/accountAccountV1Badge.js +82 -0
  17. package/funcs/accountAccountV1Badge.js.map +1 -0
  18. package/funcs/livenessLiveness.d.ts +16 -0
  19. package/funcs/livenessLiveness.d.ts.map +1 -0
  20. package/funcs/livenessLiveness.js +83 -0
  21. package/funcs/livenessLiveness.js.map +1 -0
  22. package/hooks/hooks.d.ts +24 -0
  23. package/hooks/hooks.d.ts.map +1 -0
  24. package/hooks/hooks.js +66 -0
  25. package/hooks/hooks.js.map +1 -0
  26. package/hooks/index.d.ts +3 -0
  27. package/hooks/index.d.ts.map +1 -0
  28. package/hooks/index.js +22 -0
  29. package/hooks/index.js.map +1 -0
  30. package/hooks/types.d.ts +72 -0
  31. package/hooks/types.d.ts.map +1 -0
  32. package/hooks/types.js +6 -0
  33. package/hooks/types.js.map +1 -0
  34. package/index.d.ts +3 -0
  35. package/index.d.ts.map +1 -0
  36. package/index.js +22 -0
  37. package/index.js.map +1 -0
  38. package/lib/base64.d.ts +10 -0
  39. package/lib/base64.d.ts.map +1 -0
  40. package/lib/base64.js +61 -0
  41. package/lib/base64.js.map +1 -0
  42. package/lib/config.d.ts +36 -0
  43. package/lib/config.d.ts.map +1 -0
  44. package/lib/config.js +34 -0
  45. package/lib/config.js.map +1 -0
  46. package/lib/dlv.d.ts +14 -0
  47. package/lib/dlv.d.ts.map +1 -0
  48. package/lib/dlv.js +49 -0
  49. package/lib/dlv.js.map +1 -0
  50. package/lib/encodings.d.ts +51 -0
  51. package/lib/encodings.d.ts.map +1 -0
  52. package/lib/encodings.js +343 -0
  53. package/lib/encodings.js.map +1 -0
  54. package/lib/http.d.ts +66 -0
  55. package/lib/http.d.ts.map +1 -0
  56. package/lib/http.js +217 -0
  57. package/lib/http.js.map +1 -0
  58. package/lib/is-plain-object.d.ts +2 -0
  59. package/lib/is-plain-object.d.ts.map +1 -0
  60. package/lib/is-plain-object.js +41 -0
  61. package/lib/is-plain-object.js.map +1 -0
  62. package/lib/matchers.d.ts +64 -0
  63. package/lib/matchers.d.ts.map +1 -0
  64. package/lib/matchers.js +209 -0
  65. package/lib/matchers.js.map +1 -0
  66. package/lib/primitives.d.ts +16 -0
  67. package/lib/primitives.d.ts.map +1 -0
  68. package/lib/primitives.js +29 -0
  69. package/lib/primitives.js.map +1 -0
  70. package/lib/retries.d.ts +18 -0
  71. package/lib/retries.d.ts.map +1 -0
  72. package/lib/retries.js +130 -0
  73. package/lib/retries.js.map +1 -0
  74. package/lib/schemas.d.ts +19 -0
  75. package/lib/schemas.d.ts.map +1 -0
  76. package/lib/schemas.js +59 -0
  77. package/lib/schemas.js.map +1 -0
  78. package/lib/sdks.d.ts +57 -0
  79. package/lib/sdks.d.ts.map +1 -0
  80. package/lib/sdks.js +159 -0
  81. package/lib/sdks.js.map +1 -0
  82. package/lib/security.d.ts +60 -0
  83. package/lib/security.d.ts.map +1 -0
  84. package/lib/security.js +130 -0
  85. package/lib/security.js.map +1 -0
  86. package/lib/url.d.ts +5 -0
  87. package/lib/url.d.ts.map +1 -0
  88. package/lib/url.js +25 -0
  89. package/lib/url.js.map +1 -0
  90. package/models/components/errordetail.d.ts +38 -0
  91. package/models/components/errordetail.d.ts.map +1 -0
  92. package/models/components/errordetail.js +54 -0
  93. package/models/components/errordetail.js.map +1 -0
  94. package/models/components/index.d.ts +8 -0
  95. package/models/components/index.d.ts.map +1 -0
  96. package/models/components/index.js +27 -0
  97. package/models/components/index.js.map +1 -0
  98. package/models/components/security.d.ts +27 -0
  99. package/models/components/security.d.ts.map +1 -0
  100. package/models/components/security.js +52 -0
  101. package/models/components/security.js.map +1 -0
  102. package/models/components/v1accountsappsresponsebody.d.ts +31 -0
  103. package/models/components/v1accountsappsresponsebody.d.ts.map +1 -0
  104. package/models/components/v1accountsappsresponsebody.js +66 -0
  105. package/models/components/v1accountsappsresponsebody.js.map +1 -0
  106. package/models/components/v1app.d.ts +53 -0
  107. package/models/components/v1app.d.ts.map +1 -0
  108. package/models/components/v1app.js +60 -0
  109. package/models/components/v1app.js.map +1 -0
  110. package/models/components/v1badge.d.ts +51 -0
  111. package/models/components/v1badge.d.ts.map +1 -0
  112. package/models/components/v1badge.js +79 -0
  113. package/models/components/v1badge.js.map +1 -0
  114. package/models/components/v1badgesappsresponsebody.d.ts +31 -0
  115. package/models/components/v1badgesappsresponsebody.d.ts.map +1 -0
  116. package/models/components/v1badgesappsresponsebody.js +66 -0
  117. package/models/components/v1badgesappsresponsebody.js.map +1 -0
  118. package/models/components/v1livenessresponsebody.d.ts +33 -0
  119. package/models/components/v1livenessresponsebody.d.ts.map +1 -0
  120. package/models/components/v1livenessresponsebody.js +65 -0
  121. package/models/components/v1livenessresponsebody.js.map +1 -0
  122. package/models/errors/errormodel.d.ts +98 -0
  123. package/models/errors/errormodel.d.ts.map +1 -0
  124. package/models/errors/errormodel.js +113 -0
  125. package/models/errors/errormodel.js.map +1 -0
  126. package/models/errors/httpclienterrors.d.ts +44 -0
  127. package/models/errors/httpclienterrors.d.ts.map +1 -0
  128. package/models/errors/httpclienterrors.js +78 -0
  129. package/models/errors/httpclienterrors.js.map +1 -0
  130. package/models/errors/index.d.ts +4 -0
  131. package/models/errors/index.d.ts.map +1 -0
  132. package/models/errors/index.js +23 -0
  133. package/models/errors/index.js.map +1 -0
  134. package/models/errors/sdkerror.d.ts +8 -0
  135. package/models/errors/sdkerror.d.ts.map +1 -0
  136. package/models/errors/sdkerror.js +21 -0
  137. package/models/errors/sdkerror.js.map +1 -0
  138. package/models/errors/sdkvalidationerror.d.ts +17 -0
  139. package/models/errors/sdkvalidationerror.d.ts.map +1 -0
  140. package/models/errors/sdkvalidationerror.js +107 -0
  141. package/models/errors/sdkvalidationerror.js.map +1 -0
  142. package/ns/LICENSE +201 -0
  143. package/ns/README.md +1 -0
  144. package/package.json +31 -0
  145. package/sdk/account.d.ts +13 -0
  146. package/sdk/account.d.ts.map +1 -0
  147. package/sdk/account.js +26 -0
  148. package/sdk/account.js.map +1 -0
  149. package/sdk/index.d.ts +2 -0
  150. package/sdk/index.d.ts.map +1 -0
  151. package/sdk/index.js +21 -0
  152. package/sdk/index.js.map +1 -0
  153. package/sdk/liveness.d.ts +12 -0
  154. package/sdk/liveness.d.ts.map +1 -0
  155. package/sdk/liveness.js +22 -0
  156. package/sdk/liveness.js.map +1 -0
  157. package/sdk/sdk.d.ts +10 -0
  158. package/sdk/sdk.d.ts.map +1 -0
  159. package/sdk/sdk.js +21 -0
  160. package/sdk/sdk.js.map +1 -0
  161. package/src/core.ts +13 -0
  162. package/src/funcs/accountAccountV1Apps.ts +109 -0
  163. package/src/funcs/accountAccountV1Badge.ts +109 -0
  164. package/src/funcs/livenessLiveness.ts +110 -0
  165. package/src/hooks/hooks.ts +99 -0
  166. package/src/hooks/index.ts +6 -0
  167. package/src/hooks/types.ts +89 -0
  168. package/src/index.ts +6 -0
  169. package/src/lib/base64.ts +37 -0
  170. package/src/lib/config.ts +60 -0
  171. package/src/lib/dlv.ts +53 -0
  172. package/src/lib/encodings.ts +449 -0
  173. package/src/lib/http.ts +323 -0
  174. package/src/lib/is-plain-object.ts +43 -0
  175. package/src/lib/matchers.ts +308 -0
  176. package/src/lib/primitives.ts +40 -0
  177. package/src/lib/retries.ts +187 -0
  178. package/src/lib/schemas.ts +74 -0
  179. package/src/lib/sdks.ts +261 -0
  180. package/src/lib/security.ts +198 -0
  181. package/src/lib/url.ts +33 -0
  182. package/src/models/components/errordetail.ts +58 -0
  183. package/src/models/components/index.ts +11 -0
  184. package/src/models/components/security.ts +42 -0
  185. package/src/models/components/v1accountsappsresponsebody.ts +66 -0
  186. package/src/models/components/v1app.ts +75 -0
  187. package/src/models/components/v1badge.ts +92 -0
  188. package/src/models/components/v1badgesappsresponsebody.ts +71 -0
  189. package/src/models/components/v1livenessresponsebody.ts +68 -0
  190. package/src/models/errors/errormodel.ts +174 -0
  191. package/src/models/errors/httpclienterrors.ts +62 -0
  192. package/src/models/errors/index.ts +7 -0
  193. package/src/models/errors/sdkerror.ts +25 -0
  194. package/src/models/errors/sdkvalidationerror.ts +95 -0
  195. package/src/sdk/account.ts +25 -0
  196. package/src/sdk/index.ts +5 -0
  197. package/src/sdk/liveness.ts +20 -0
  198. package/src/sdk/sdk.ts +19 -0
  199. package/src/types/blobs.ts +31 -0
  200. package/src/types/enums.ts +16 -0
  201. package/src/types/fp.ts +50 -0
  202. package/src/types/index.ts +11 -0
  203. package/src/types/operations.ts +99 -0
  204. package/src/types/rfcdate.ts +54 -0
  205. package/types/blobs.d.ts +4 -0
  206. package/types/blobs.d.ts.map +1 -0
  207. package/types/blobs.js +52 -0
  208. package/types/blobs.js.map +1 -0
  209. package/types/enums.d.ts +12 -0
  210. package/types/enums.d.ts.map +1 -0
  211. package/types/enums.js +10 -0
  212. package/types/enums.js.map +1 -0
  213. package/types/fp.d.ts +31 -0
  214. package/types/fp.d.ts.map +1 -0
  215. package/types/fp.js +37 -0
  216. package/types/fp.js.map +1 -0
  217. package/types/index.d.ts +8 -0
  218. package/types/index.d.ts.map +1 -0
  219. package/types/index.js +16 -0
  220. package/types/index.js.map +1 -0
  221. package/types/operations.d.ts +25 -0
  222. package/types/operations.d.ts.map +1 -0
  223. package/types/operations.js +80 -0
  224. package/types/operations.js.map +1 -0
  225. package/types/rfcdate.d.ts +21 -0
  226. package/types/rfcdate.d.ts.map +1 -0
  227. package/types/rfcdate.js +46 -0
  228. package/types/rfcdate.js.map +1 -0
@@ -0,0 +1,187 @@
1
+ /*
2
+ * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ */
4
+
5
+ import { isConnectionError, isTimeoutError } from "./http.js";
6
+
7
+ export type BackoffStrategy = {
8
+ initialInterval: number;
9
+ maxInterval: number;
10
+ exponent: number;
11
+ maxElapsedTime: number;
12
+ };
13
+
14
+ const defaultBackoff: BackoffStrategy = {
15
+ initialInterval: 500,
16
+ maxInterval: 60000,
17
+ exponent: 1.5,
18
+ maxElapsedTime: 3600000,
19
+ };
20
+
21
+ export type RetryConfig =
22
+ | { strategy: "none" }
23
+ | {
24
+ strategy: "backoff";
25
+ backoff?: BackoffStrategy;
26
+ retryConnectionErrors?: boolean;
27
+ };
28
+
29
+ class PermanentError extends Error {
30
+ inner: unknown;
31
+
32
+ constructor(inner: unknown) {
33
+ super("Permanent error");
34
+ this.inner = inner;
35
+
36
+ Object.setPrototypeOf(this, PermanentError.prototype);
37
+ }
38
+ }
39
+
40
+ class TemporaryError extends Error {
41
+ res: Response;
42
+
43
+ constructor(res: Response) {
44
+ super("Temporary error");
45
+ this.res = res;
46
+
47
+ Object.setPrototypeOf(this, TemporaryError.prototype);
48
+ }
49
+ }
50
+
51
+ export async function retry(
52
+ fetchFn: () => Promise<Response>,
53
+ options: {
54
+ config: RetryConfig;
55
+ statusCodes: string[];
56
+ },
57
+ ): Promise<Response> {
58
+ switch (options.config.strategy) {
59
+ case "backoff":
60
+ return retryBackoff(
61
+ wrapFetcher(fetchFn, {
62
+ statusCodes: options.statusCodes,
63
+ retryConnectionErrors: !!options.config.retryConnectionErrors,
64
+ }),
65
+ options.config.backoff ?? defaultBackoff,
66
+ );
67
+ default:
68
+ return await fetchFn();
69
+ }
70
+ }
71
+
72
+ function wrapFetcher(
73
+ fn: () => Promise<Response>,
74
+ options: {
75
+ statusCodes: string[];
76
+ retryConnectionErrors: boolean;
77
+ },
78
+ ): () => Promise<Response> {
79
+ return async () => {
80
+ try {
81
+ const res = await fn();
82
+ if (isRetryableResponse(res, options.statusCodes)) {
83
+ throw new TemporaryError(res);
84
+ }
85
+
86
+ return res;
87
+ } catch (err) {
88
+ if (err instanceof TemporaryError) {
89
+ throw err;
90
+ }
91
+
92
+ if (
93
+ options.retryConnectionErrors &&
94
+ (isTimeoutError(err) || isConnectionError(err))
95
+ ) {
96
+ throw err;
97
+ }
98
+
99
+ throw new PermanentError(err);
100
+ }
101
+ };
102
+ }
103
+
104
+ const codeRangeRE = new RegExp("^[0-9]xx$", "i");
105
+
106
+ function isRetryableResponse(res: Response, statusCodes: string[]): boolean {
107
+ const actual = `${res.status}`;
108
+
109
+ return statusCodes.some((code) => {
110
+ if (!codeRangeRE.test(code)) {
111
+ return code === actual;
112
+ }
113
+
114
+ const expectFamily = code.charAt(0);
115
+ if (!expectFamily) {
116
+ throw new Error("Invalid status code range");
117
+ }
118
+
119
+ const actualFamily = actual.charAt(0);
120
+ if (!actualFamily) {
121
+ throw new Error(`Invalid response status code: ${actual}`);
122
+ }
123
+
124
+ return actualFamily === expectFamily;
125
+ });
126
+ }
127
+
128
+ async function retryBackoff(
129
+ fn: () => Promise<Response>,
130
+ strategy: BackoffStrategy,
131
+ ): Promise<Response> {
132
+ const { maxElapsedTime, initialInterval, exponent, maxInterval } = strategy;
133
+
134
+ const start = Date.now();
135
+ let x = 0;
136
+
137
+ // eslint-disable-next-line no-constant-condition
138
+ while (true) {
139
+ try {
140
+ const res = await fn();
141
+ return res;
142
+ } catch (err) {
143
+ if (err instanceof PermanentError) {
144
+ throw err.inner;
145
+ }
146
+ const elapsed = Date.now() - start;
147
+ if (elapsed > maxElapsedTime) {
148
+ if (err instanceof TemporaryError) {
149
+ return err.res;
150
+ }
151
+
152
+ throw err;
153
+ }
154
+
155
+ let retryInterval = 0;
156
+ if (err instanceof TemporaryError && err.res && err.res.headers) {
157
+ const retryVal = err.res.headers.get("retry-after") || "";
158
+ if (retryVal != "") {
159
+ const parsedNumber = Number(retryVal);
160
+ if (!isNaN(parsedNumber) && Number.isInteger(parsedNumber)) {
161
+ retryInterval = parsedNumber * 1000;
162
+ } else {
163
+ const parsedDate = Date.parse(retryVal);
164
+ if (!isNaN(parsedDate)) {
165
+ const deltaMS = parsedDate - Date.now();
166
+ retryInterval = deltaMS > 0 ? Math.ceil(deltaMS) : 0;
167
+ }
168
+ }
169
+ }
170
+ }
171
+
172
+ if (retryInterval == 0) {
173
+ retryInterval =
174
+ initialInterval * Math.pow(x, exponent) + Math.random() * 1000;
175
+ }
176
+
177
+ const d = Math.min(retryInterval, maxInterval);
178
+
179
+ await delay(d);
180
+ x++;
181
+ }
182
+ }
183
+ }
184
+
185
+ async function delay(delay: number): Promise<void> {
186
+ return new Promise((resolve) => setTimeout(resolve, delay));
187
+ }
@@ -0,0 +1,74 @@
1
+ /*
2
+ * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ */
4
+
5
+ import { output, ZodEffects, ZodError, ZodObject, ZodRawShape, ZodTypeAny } from "zod";
6
+ import { SDKValidationError } from "../models/errors/sdkvalidationerror.js";
7
+ import { ERR, OK, Result } from "../types/fp.js";
8
+
9
+ /**
10
+ * Utility function that executes some code which may throw a ZodError. It
11
+ * intercepts this error and converts it to an SDKValidationError so as to not
12
+ * leak Zod implementation details to user code.
13
+ */
14
+ export function parse<Inp, Out>(rawValue: Inp, fn: (value: Inp) => Out, errorMessage: string): Out {
15
+ try {
16
+ return fn(rawValue);
17
+ } catch (err) {
18
+ if (err instanceof ZodError) {
19
+ throw new SDKValidationError(errorMessage, err, rawValue);
20
+ }
21
+ throw err;
22
+ }
23
+ }
24
+
25
+ /**
26
+ * Utility function that executes some code which may result in a ZodError. It
27
+ * intercepts this error and converts it to an SDKValidationError so as to not
28
+ * leak Zod implementation details to user code.
29
+ */
30
+ export function safeParse<Inp, Out>(
31
+ rawValue: Inp,
32
+ fn: (value: Inp) => Out,
33
+ errorMessage: string
34
+ ): Result<Out, SDKValidationError> {
35
+ try {
36
+ return OK(fn(rawValue));
37
+ } catch (err) {
38
+ return ERR(new SDKValidationError(errorMessage, err, rawValue));
39
+ }
40
+ }
41
+
42
+ export function collectExtraKeys<
43
+ Shape extends ZodRawShape,
44
+ Catchall extends ZodTypeAny,
45
+ K extends string
46
+ >(
47
+ obj: ZodObject<Shape, "strip", Catchall>,
48
+ extrasKey: K
49
+ ): ZodEffects<
50
+ typeof obj,
51
+ output<ZodObject<Shape, "strict">> & {
52
+ [k in K]: Record<string, output<Catchall>>;
53
+ }
54
+ > {
55
+ return obj.transform((val) => {
56
+ const extras: Record<string, output<Catchall>> = {};
57
+ const { shape } = obj;
58
+ for (const [key] of Object.entries(val)) {
59
+ if (key in shape) {
60
+ continue;
61
+ }
62
+
63
+ const v = val[key];
64
+ if (typeof v === "undefined") {
65
+ continue;
66
+ }
67
+
68
+ extras[key] = v;
69
+ delete val[key];
70
+ }
71
+
72
+ return { ...val, [extrasKey]: extras };
73
+ });
74
+ }
@@ -0,0 +1,261 @@
1
+ /*
2
+ * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ */
4
+
5
+ import {
6
+ HTTPClient,
7
+ matchStatusCode,
8
+ isAbortError,
9
+ isTimeoutError,
10
+ isConnectionError,
11
+ } from "./http.js";
12
+ import { SecurityState } from "./security.js";
13
+ import { retry, RetryConfig } from "./retries.js";
14
+ import { encodeForm } from "./encodings.js";
15
+ import { stringToBase64 } from "./base64.js";
16
+ import { SDKOptions, SDK_METADATA, serverURLFromOptions } from "./config.js";
17
+ import { SDKHooks } from "../hooks/hooks.js";
18
+ import { HookContext } from "../hooks/types.js";
19
+ import {
20
+ ConnectionError,
21
+ InvalidRequestError,
22
+ RequestAbortedError,
23
+ RequestTimeoutError,
24
+ UnexpectedClientError,
25
+ } from "../models/errors/httpclienterrors.js";
26
+ import { ERR, OK, Result } from "../types/fp.js";
27
+
28
+ export type RequestOptions = {
29
+ /**
30
+ * Sets a timeout, in milliseconds, on HTTP requests made by an SDK method. If
31
+ * `fetchOptions.signal` is set then it will take precedence over this option.
32
+ */
33
+ timeoutMs?: number;
34
+ /**
35
+ * Set or override a retry policy on HTTP calls.
36
+ */
37
+ retries?: RetryConfig;
38
+ /**
39
+ * Specifies the status codes which should be retried using the given retry policy.
40
+ */
41
+ retryCodes?: string[];
42
+ /**
43
+ * Sets various request options on the `fetch` call made by an SDK method.
44
+ *
45
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Request/Request#options|Request}
46
+ */
47
+ fetchOptions?: Omit<RequestInit, "method" | "body">;
48
+ };
49
+
50
+ type RequestConfig = {
51
+ method: string;
52
+ path: string;
53
+ baseURL?: string | URL;
54
+ query?: string;
55
+ body?: RequestInit["body"];
56
+ headers?: HeadersInit;
57
+ security?: SecurityState | null;
58
+ uaHeader?: string;
59
+ timeoutMs?: number;
60
+ };
61
+
62
+ const gt: unknown = typeof globalThis === "undefined" ? null : globalThis;
63
+ const webWorkerLike =
64
+ typeof gt === "object" &&
65
+ gt != null &&
66
+ "importScripts" in gt &&
67
+ typeof gt["importScripts"] === "function";
68
+ const isBrowserLike =
69
+ webWorkerLike ||
70
+ (typeof navigator !== "undefined" && "serviceWorker" in navigator) ||
71
+ (typeof window === "object" && typeof window.document !== "undefined");
72
+
73
+ export class ClientSDK {
74
+ private readonly httpClient: HTTPClient;
75
+ protected readonly baseURL: URL | null;
76
+ protected readonly hooks$: SDKHooks;
77
+ public readonly options$: SDKOptions & { hooks?: SDKHooks };
78
+
79
+ constructor(options: SDKOptions = {}) {
80
+ const opt = options as unknown;
81
+ if (
82
+ typeof opt === "object" &&
83
+ opt != null &&
84
+ "hooks" in opt &&
85
+ opt.hooks instanceof SDKHooks
86
+ ) {
87
+ this.hooks$ = opt.hooks;
88
+ } else {
89
+ this.hooks$ = new SDKHooks();
90
+ }
91
+ this.options$ = { ...options, hooks: this.hooks$ };
92
+
93
+ const url = serverURLFromOptions(options);
94
+ if (url) {
95
+ url.pathname = url.pathname.replace(/\/+$/, "") + "/";
96
+ }
97
+ const { baseURL, client } = this.hooks$.sdkInit({
98
+ baseURL: url,
99
+ client: options.httpClient || new HTTPClient(),
100
+ });
101
+ this.baseURL = baseURL;
102
+ this.httpClient = client;
103
+ }
104
+
105
+ public createRequest$(
106
+ context: HookContext,
107
+ conf: RequestConfig,
108
+ options?: RequestOptions
109
+ ): Result<Request, InvalidRequestError | UnexpectedClientError> {
110
+ const { method, path, query, headers: opHeaders, security } = conf;
111
+
112
+ const base = conf.baseURL ?? this.baseURL;
113
+ if (!base) {
114
+ return ERR(new InvalidRequestError("No base URL provided for operation"));
115
+ }
116
+ const reqURL = new URL(base);
117
+ const inputURL = new URL(path, reqURL);
118
+
119
+ if (path) {
120
+ reqURL.pathname += inputURL.pathname.replace(/^\/+/, "");
121
+ }
122
+
123
+ let finalQuery = query || "";
124
+
125
+ const secQuery: string[] = [];
126
+ for (const [k, v] of Object.entries(security?.queryParams || {})) {
127
+ secQuery.push(encodeForm(k, v, { charEncoding: "percent" }));
128
+ }
129
+ if (secQuery.length) {
130
+ finalQuery += `&${secQuery.join("&")}`;
131
+ }
132
+
133
+ if (finalQuery) {
134
+ const q = finalQuery.startsWith("&") ? finalQuery.slice(1) : finalQuery;
135
+ reqURL.search = `?${q}`;
136
+ }
137
+
138
+ const headers = new Headers(opHeaders);
139
+
140
+ const username = security?.basic.username;
141
+ const password = security?.basic.password;
142
+ if (username != null || password != null) {
143
+ const encoded = stringToBase64([username || "", password || ""].join(":"));
144
+ headers.set("Authorization", `Basic ${encoded}`);
145
+ }
146
+
147
+ const securityHeaders = new Headers(security?.headers || {});
148
+ for (const [k, v] of securityHeaders) {
149
+ headers.set(k, v);
150
+ }
151
+
152
+ let cookie = headers.get("cookie") || "";
153
+ for (const [k, v] of Object.entries(security?.cookies || {})) {
154
+ cookie += `; ${k}=${v}`;
155
+ }
156
+ cookie = cookie.startsWith("; ") ? cookie.slice(2) : cookie;
157
+ headers.set("cookie", cookie);
158
+
159
+ const userHeaders = new Headers(options?.fetchOptions?.headers);
160
+ for (const [k, v] of userHeaders) {
161
+ headers.set(k, v);
162
+ }
163
+
164
+ // Only set user agent header in non-browser-like environments since CORS
165
+ // policy disallows setting it in browsers e.g. Chrome throws an error.
166
+ if (!isBrowserLike) {
167
+ headers.set(conf.uaHeader ?? "user-agent", SDK_METADATA.userAgent);
168
+ }
169
+
170
+ let fetchOptions = options?.fetchOptions;
171
+ if (!fetchOptions?.signal && conf.timeoutMs && conf.timeoutMs > 0) {
172
+ const timeoutSignal = AbortSignal.timeout(conf.timeoutMs);
173
+ if (!fetchOptions) {
174
+ fetchOptions = { signal: timeoutSignal };
175
+ } else {
176
+ fetchOptions.signal = timeoutSignal;
177
+ }
178
+ }
179
+
180
+ let input;
181
+ try {
182
+ input = this.hooks$.beforeCreateRequest(context, {
183
+ url: reqURL,
184
+ options: {
185
+ ...fetchOptions,
186
+ body: conf.body ?? null,
187
+ headers,
188
+ method,
189
+ },
190
+ });
191
+ } catch (err: unknown) {
192
+ return ERR(
193
+ new UnexpectedClientError("Create request hook failed to execute", { cause: err })
194
+ );
195
+ }
196
+
197
+ return OK(new Request(input.url, input.options));
198
+ }
199
+
200
+ public async do$(
201
+ request: Request,
202
+ options: {
203
+ context: HookContext;
204
+ errorCodes: number | string | (number | string)[];
205
+ retryConfig?: RetryConfig | undefined;
206
+ retryCodes?: string[] | undefined;
207
+ }
208
+ ): Promise<
209
+ Result<
210
+ Response,
211
+ RequestAbortedError | RequestTimeoutError | ConnectionError | UnexpectedClientError
212
+ >
213
+ > {
214
+ const { context, errorCodes } = options;
215
+ const retryConfig = options.retryConfig || { strategy: "none" };
216
+ const retryCodes = options.retryCodes || [];
217
+
218
+ return retry(
219
+ async () => {
220
+ const req = request.clone();
221
+
222
+ let response = await this.httpClient.request(
223
+ await this.hooks$.beforeRequest(context, req)
224
+ );
225
+
226
+ if (matchStatusCode(response, errorCodes)) {
227
+ const result = await this.hooks$.afterError(context, response, null);
228
+ if (result.error) {
229
+ throw result.error;
230
+ }
231
+ response = result.response || response;
232
+ } else {
233
+ response = await this.hooks$.afterSuccess(context, response);
234
+ }
235
+
236
+ return response;
237
+ },
238
+ { config: retryConfig, statusCodes: retryCodes }
239
+ ).then(
240
+ (r) => OK(r),
241
+ (err) => {
242
+ switch (true) {
243
+ case isAbortError(err):
244
+ return ERR(
245
+ new RequestAbortedError("Request aborted by client", { cause: err })
246
+ );
247
+ case isTimeoutError(err):
248
+ return ERR(new RequestTimeoutError("Request timed out", { cause: err }));
249
+ case isConnectionError(err):
250
+ return ERR(new ConnectionError("Unable to make request", { cause: err }));
251
+ default:
252
+ return ERR(
253
+ new UnexpectedClientError("Unexpected HTTP client error", {
254
+ cause: err,
255
+ })
256
+ );
257
+ }
258
+ }
259
+ );
260
+ }
261
+ }