@barekey/sdk 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.
- package/LICENSE +28 -0
- package/README.md +21 -0
- package/dist/client.d.ts +41 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +302 -0
- package/dist/errors.d.ts +461 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +343 -0
- package/dist/handle.d.ts +20 -0
- package/dist/handle.d.ts.map +1 -0
- package/dist/handle.js +35 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/internal/cache.d.ts +13 -0
- package/dist/internal/cache.d.ts.map +1 -0
- package/dist/internal/cache.js +24 -0
- package/dist/internal/evaluate.d.ts +7 -0
- package/dist/internal/evaluate.d.ts.map +1 -0
- package/dist/internal/evaluate.js +176 -0
- package/dist/internal/http.d.ts +19 -0
- package/dist/internal/http.d.ts.map +1 -0
- package/dist/internal/http.js +92 -0
- package/dist/internal/node-runtime.d.ts +19 -0
- package/dist/internal/node-runtime.d.ts.map +1 -0
- package/dist/internal/node-runtime.js +422 -0
- package/dist/internal/requirements.d.ts +3 -0
- package/dist/internal/requirements.d.ts.map +1 -0
- package/dist/internal/requirements.js +40 -0
- package/dist/internal/runtime.d.ts +15 -0
- package/dist/internal/runtime.d.ts.map +1 -0
- package/dist/internal/runtime.js +135 -0
- package/dist/internal/ttl.d.ts +4 -0
- package/dist/internal/ttl.d.ts.map +1 -0
- package/dist/internal/ttl.js +30 -0
- package/dist/internal/typegen.d.ts +25 -0
- package/dist/internal/typegen.d.ts.map +1 -0
- package/dist/internal/typegen.js +75 -0
- package/dist/types.d.ts +130 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/generated.d.ts +16 -0
- package/index.d.ts +2 -0
- package/package.json +42 -0
- package/src/client.ts +422 -0
- package/src/errors.ts +420 -0
- package/src/handle.ts +67 -0
- package/src/index.ts +60 -0
- package/src/internal/cache.ts +33 -0
- package/src/internal/evaluate.ts +232 -0
- package/src/internal/http.ts +134 -0
- package/src/internal/node-runtime.ts +581 -0
- package/src/internal/requirements.ts +57 -0
- package/src/internal/runtime.ts +199 -0
- package/src/internal/ttl.ts +41 -0
- package/src/internal/typegen.ts +124 -0
- package/src/types.ts +189 -0
package/src/errors.ts
ADDED
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
import type { BarekeyErrorCode, BarekeyTemporalInstant } from "./types.js";
|
|
2
|
+
|
|
3
|
+
const INT64_MIN = BigInt("-9223372036854775808");
|
|
4
|
+
const INT64_MAX = BigInt("9223372036854775807");
|
|
5
|
+
|
|
6
|
+
type BarekeyErrorInit = {
|
|
7
|
+
message?: string;
|
|
8
|
+
requestId?: string | null;
|
|
9
|
+
status?: number | null;
|
|
10
|
+
cause?: unknown;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type BarekeyErrorDescriptor = {
|
|
14
|
+
code: BarekeyErrorCode;
|
|
15
|
+
name: string;
|
|
16
|
+
description: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const ERROR_DESCRIPTORS = {
|
|
20
|
+
FS_NOT_AVAILABLE: {
|
|
21
|
+
code: "FS_NOT_AVAILABLE",
|
|
22
|
+
name: "FsNotAvailableError",
|
|
23
|
+
description: "A filesystem is not available.",
|
|
24
|
+
},
|
|
25
|
+
NO_CONFIGURATION_PROVIDED: {
|
|
26
|
+
code: "NO_CONFIGURATION_PROVIDED",
|
|
27
|
+
name: "NoConfigurationProvidedError",
|
|
28
|
+
description: "No Barekey configuration was provided.",
|
|
29
|
+
},
|
|
30
|
+
INVALID_CONFIGURATION_PROVIDED: {
|
|
31
|
+
code: "INVALID_CONFIGURATION_PROVIDED",
|
|
32
|
+
name: "InvalidConfigurationProvidedError",
|
|
33
|
+
description: "Invalid Barekey configuration was provided.",
|
|
34
|
+
},
|
|
35
|
+
NO_CREDENTIALS_PROVIDED: {
|
|
36
|
+
code: "NO_CREDENTIALS_PROVIDED",
|
|
37
|
+
name: "NoCredentialsProvidedError",
|
|
38
|
+
description: "No Barekey credentials were provided.",
|
|
39
|
+
},
|
|
40
|
+
INVALID_CREDENTIALS_PROVIDED: {
|
|
41
|
+
code: "INVALID_CREDENTIALS_PROVIDED",
|
|
42
|
+
name: "InvalidCredentialsProvidedError",
|
|
43
|
+
description: "Invalid Barekey credentials were provided.",
|
|
44
|
+
},
|
|
45
|
+
INVALID_DYNAMIC_OPTIONS: {
|
|
46
|
+
code: "INVALID_DYNAMIC_OPTIONS",
|
|
47
|
+
name: "InvalidDynamicOptionsError",
|
|
48
|
+
description: "Invalid dynamic options were provided.",
|
|
49
|
+
},
|
|
50
|
+
REQUIREMENTS_VALIDATION_FAILED: {
|
|
51
|
+
code: "REQUIREMENTS_VALIDATION_FAILED",
|
|
52
|
+
name: "RequirementsValidationFailedError",
|
|
53
|
+
description: "Barekey requirements validation failed.",
|
|
54
|
+
},
|
|
55
|
+
NETWORK_ERROR: {
|
|
56
|
+
code: "NETWORK_ERROR",
|
|
57
|
+
name: "NetworkError",
|
|
58
|
+
description: "A Barekey network request failed.",
|
|
59
|
+
},
|
|
60
|
+
COERCE_FAILED: {
|
|
61
|
+
code: "COERCE_FAILED",
|
|
62
|
+
name: "CoerceFailedError",
|
|
63
|
+
description: "Barekey could not coerce the resolved value.",
|
|
64
|
+
},
|
|
65
|
+
TEMPORAL_NOT_AVAILABLE: {
|
|
66
|
+
code: "TEMPORAL_NOT_AVAILABLE",
|
|
67
|
+
name: "TemporalNotAvailableError",
|
|
68
|
+
description: "Temporal is not available in this runtime.",
|
|
69
|
+
},
|
|
70
|
+
SDK_MODULE_NOT_FOUND: {
|
|
71
|
+
code: "SDK_MODULE_NOT_FOUND",
|
|
72
|
+
name: "SdkModuleNotFoundError",
|
|
73
|
+
description: "The installed @barekey/sdk module could not be found.",
|
|
74
|
+
},
|
|
75
|
+
TYPEGEN_UNSUPPORTED_SDK: {
|
|
76
|
+
code: "TYPEGEN_UNSUPPORTED_SDK",
|
|
77
|
+
name: "TypegenUnsupportedSdkError",
|
|
78
|
+
description: "The installed @barekey/sdk module does not support in-module typegen.",
|
|
79
|
+
},
|
|
80
|
+
TYPEGEN_READ_FAILED: {
|
|
81
|
+
code: "TYPEGEN_READ_FAILED",
|
|
82
|
+
name: "TypegenReadFailedError",
|
|
83
|
+
description: "Barekey could not read the generated SDK types.",
|
|
84
|
+
},
|
|
85
|
+
TYPEGEN_WRITE_FAILED: {
|
|
86
|
+
code: "TYPEGEN_WRITE_FAILED",
|
|
87
|
+
name: "TypegenWriteFailedError",
|
|
88
|
+
description: "Barekey could not write the generated SDK types.",
|
|
89
|
+
},
|
|
90
|
+
UNAUTHORIZED: {
|
|
91
|
+
code: "UNAUTHORIZED",
|
|
92
|
+
name: "UnauthorizedError",
|
|
93
|
+
description: "The provided Barekey credentials were rejected.",
|
|
94
|
+
},
|
|
95
|
+
INVALID_ORG_SCOPE: {
|
|
96
|
+
code: "INVALID_ORG_SCOPE",
|
|
97
|
+
name: "InvalidOrgScopeError",
|
|
98
|
+
description: "The requested organization scope is invalid.",
|
|
99
|
+
},
|
|
100
|
+
ORG_SCOPE_INVALID: {
|
|
101
|
+
code: "ORG_SCOPE_INVALID",
|
|
102
|
+
name: "OrgScopeInvalidError",
|
|
103
|
+
description: "The authenticated organization scope is invalid.",
|
|
104
|
+
},
|
|
105
|
+
INVALID_JSON: {
|
|
106
|
+
code: "INVALID_JSON",
|
|
107
|
+
name: "InvalidJsonError",
|
|
108
|
+
description: "The Barekey request body was invalid JSON.",
|
|
109
|
+
},
|
|
110
|
+
INVALID_REQUEST: {
|
|
111
|
+
code: "INVALID_REQUEST",
|
|
112
|
+
name: "InvalidRequestError",
|
|
113
|
+
description: "The Barekey request was invalid.",
|
|
114
|
+
},
|
|
115
|
+
VARIABLE_NOT_FOUND: {
|
|
116
|
+
code: "VARIABLE_NOT_FOUND",
|
|
117
|
+
name: "VariableNotFoundError",
|
|
118
|
+
description: "The requested Barekey variable was not found.",
|
|
119
|
+
},
|
|
120
|
+
EVALUATION_FAILED: {
|
|
121
|
+
code: "EVALUATION_FAILED",
|
|
122
|
+
name: "EvaluationFailedError",
|
|
123
|
+
description: "Barekey could not evaluate the requested variable.",
|
|
124
|
+
},
|
|
125
|
+
USAGE_LIMIT_EXCEEDED: {
|
|
126
|
+
code: "USAGE_LIMIT_EXCEEDED",
|
|
127
|
+
name: "UsageLimitExceededError",
|
|
128
|
+
description: "The Barekey usage limit has been exceeded.",
|
|
129
|
+
},
|
|
130
|
+
BILLING_UNAVAILABLE: {
|
|
131
|
+
code: "BILLING_UNAVAILABLE",
|
|
132
|
+
name: "BillingUnavailableError",
|
|
133
|
+
description: "Barekey billing is temporarily unavailable.",
|
|
134
|
+
},
|
|
135
|
+
DEVICE_CODE_NOT_FOUND: {
|
|
136
|
+
code: "DEVICE_CODE_NOT_FOUND",
|
|
137
|
+
name: "DeviceCodeNotFoundError",
|
|
138
|
+
description: "The Barekey device code was not found.",
|
|
139
|
+
},
|
|
140
|
+
DEVICE_CODE_EXPIRED: {
|
|
141
|
+
code: "DEVICE_CODE_EXPIRED",
|
|
142
|
+
name: "DeviceCodeExpiredError",
|
|
143
|
+
description: "The Barekey device code has expired.",
|
|
144
|
+
},
|
|
145
|
+
USER_CODE_INVALID: {
|
|
146
|
+
code: "USER_CODE_INVALID",
|
|
147
|
+
name: "UserCodeInvalidError",
|
|
148
|
+
description: "The Barekey user code is invalid.",
|
|
149
|
+
},
|
|
150
|
+
INVALID_REFRESH_TOKEN: {
|
|
151
|
+
code: "INVALID_REFRESH_TOKEN",
|
|
152
|
+
name: "InvalidRefreshTokenError",
|
|
153
|
+
description: "The Barekey refresh token is invalid.",
|
|
154
|
+
},
|
|
155
|
+
UNKNOWN_ERROR: {
|
|
156
|
+
code: "UNKNOWN_ERROR",
|
|
157
|
+
name: "UnknownError",
|
|
158
|
+
description: "An unknown Barekey error occurred.",
|
|
159
|
+
},
|
|
160
|
+
} as const satisfies Record<BarekeyErrorCode, BarekeyErrorDescriptor>;
|
|
161
|
+
|
|
162
|
+
function toDocsSlug(code: BarekeyErrorCode): string {
|
|
163
|
+
return code.toLowerCase().replaceAll("_", "-");
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function docsUrlForErrorCode(code: BarekeyErrorCode): string {
|
|
167
|
+
return `https://docs.barekey.dev/errors/${toDocsSlug(code)}`;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function formatBarekeyErrorMessage(code: BarekeyErrorCode, description: string): string {
|
|
171
|
+
return `[${code}]: ${description}\n${docsUrlForErrorCode(code)}`;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export class BarekeyError extends Error {
|
|
175
|
+
readonly code: BarekeyErrorCode;
|
|
176
|
+
readonly docsUrl: string;
|
|
177
|
+
readonly requestId: string | null;
|
|
178
|
+
readonly status: number | null;
|
|
179
|
+
override readonly cause: unknown;
|
|
180
|
+
|
|
181
|
+
constructor(input: {
|
|
182
|
+
code: BarekeyErrorCode;
|
|
183
|
+
name: string;
|
|
184
|
+
message: string;
|
|
185
|
+
requestId?: string | null;
|
|
186
|
+
status?: number | null;
|
|
187
|
+
cause?: unknown;
|
|
188
|
+
}) {
|
|
189
|
+
super(formatBarekeyErrorMessage(input.code, input.message));
|
|
190
|
+
this.name = input.name;
|
|
191
|
+
this.code = input.code;
|
|
192
|
+
this.docsUrl = docsUrlForErrorCode(input.code);
|
|
193
|
+
this.requestId = input.requestId ?? null;
|
|
194
|
+
this.status = input.status ?? null;
|
|
195
|
+
this.cause = input.cause;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function buildErrorClass<const TCode extends BarekeyErrorCode>(descriptor: {
|
|
200
|
+
code: TCode;
|
|
201
|
+
name: string;
|
|
202
|
+
description: string;
|
|
203
|
+
}) {
|
|
204
|
+
return class extends BarekeyError {
|
|
205
|
+
constructor(input: BarekeyErrorInit = {}) {
|
|
206
|
+
super({
|
|
207
|
+
code: descriptor.code,
|
|
208
|
+
name: descriptor.name,
|
|
209
|
+
message: input.message ?? descriptor.description,
|
|
210
|
+
requestId: input.requestId,
|
|
211
|
+
status: input.status,
|
|
212
|
+
cause: input.cause,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export const FsNotAvailableError = buildErrorClass(ERROR_DESCRIPTORS.FS_NOT_AVAILABLE);
|
|
219
|
+
export const NoConfigurationProvidedError = buildErrorClass(
|
|
220
|
+
ERROR_DESCRIPTORS.NO_CONFIGURATION_PROVIDED,
|
|
221
|
+
);
|
|
222
|
+
export const InvalidConfigurationProvidedError = buildErrorClass(
|
|
223
|
+
ERROR_DESCRIPTORS.INVALID_CONFIGURATION_PROVIDED,
|
|
224
|
+
);
|
|
225
|
+
export const NoCredentialsProvidedError = buildErrorClass(
|
|
226
|
+
ERROR_DESCRIPTORS.NO_CREDENTIALS_PROVIDED,
|
|
227
|
+
);
|
|
228
|
+
export const InvalidCredentialsProvidedError = buildErrorClass(
|
|
229
|
+
ERROR_DESCRIPTORS.INVALID_CREDENTIALS_PROVIDED,
|
|
230
|
+
);
|
|
231
|
+
export const InvalidDynamicOptionsError = buildErrorClass(
|
|
232
|
+
ERROR_DESCRIPTORS.INVALID_DYNAMIC_OPTIONS,
|
|
233
|
+
);
|
|
234
|
+
export const RequirementsValidationFailedError = buildErrorClass(
|
|
235
|
+
ERROR_DESCRIPTORS.REQUIREMENTS_VALIDATION_FAILED,
|
|
236
|
+
);
|
|
237
|
+
export const NetworkError = buildErrorClass(ERROR_DESCRIPTORS.NETWORK_ERROR);
|
|
238
|
+
export const CoerceFailedError = buildErrorClass(ERROR_DESCRIPTORS.COERCE_FAILED);
|
|
239
|
+
export const TemporalNotAvailableError = buildErrorClass(ERROR_DESCRIPTORS.TEMPORAL_NOT_AVAILABLE);
|
|
240
|
+
export const SdkModuleNotFoundError = buildErrorClass(ERROR_DESCRIPTORS.SDK_MODULE_NOT_FOUND);
|
|
241
|
+
export const TypegenUnsupportedSdkError = buildErrorClass(
|
|
242
|
+
ERROR_DESCRIPTORS.TYPEGEN_UNSUPPORTED_SDK,
|
|
243
|
+
);
|
|
244
|
+
export const TypegenReadFailedError = buildErrorClass(ERROR_DESCRIPTORS.TYPEGEN_READ_FAILED);
|
|
245
|
+
export const TypegenWriteFailedError = buildErrorClass(ERROR_DESCRIPTORS.TYPEGEN_WRITE_FAILED);
|
|
246
|
+
export const UnauthorizedError = buildErrorClass(ERROR_DESCRIPTORS.UNAUTHORIZED);
|
|
247
|
+
export const InvalidOrgScopeError = buildErrorClass(ERROR_DESCRIPTORS.INVALID_ORG_SCOPE);
|
|
248
|
+
export const OrgScopeInvalidError = buildErrorClass(ERROR_DESCRIPTORS.ORG_SCOPE_INVALID);
|
|
249
|
+
export const InvalidJsonError = buildErrorClass(ERROR_DESCRIPTORS.INVALID_JSON);
|
|
250
|
+
export const InvalidRequestError = buildErrorClass(ERROR_DESCRIPTORS.INVALID_REQUEST);
|
|
251
|
+
export const VariableNotFoundError = buildErrorClass(ERROR_DESCRIPTORS.VARIABLE_NOT_FOUND);
|
|
252
|
+
export const EvaluationFailedError = buildErrorClass(ERROR_DESCRIPTORS.EVALUATION_FAILED);
|
|
253
|
+
export const UsageLimitExceededError = buildErrorClass(ERROR_DESCRIPTORS.USAGE_LIMIT_EXCEEDED);
|
|
254
|
+
export const BillingUnavailableError = buildErrorClass(ERROR_DESCRIPTORS.BILLING_UNAVAILABLE);
|
|
255
|
+
export const DeviceCodeNotFoundError = buildErrorClass(ERROR_DESCRIPTORS.DEVICE_CODE_NOT_FOUND);
|
|
256
|
+
export const DeviceCodeExpiredError = buildErrorClass(ERROR_DESCRIPTORS.DEVICE_CODE_EXPIRED);
|
|
257
|
+
export const UserCodeInvalidError = buildErrorClass(ERROR_DESCRIPTORS.USER_CODE_INVALID);
|
|
258
|
+
export const InvalidRefreshTokenError = buildErrorClass(ERROR_DESCRIPTORS.INVALID_REFRESH_TOKEN);
|
|
259
|
+
export const UnknownError = buildErrorClass(ERROR_DESCRIPTORS.UNKNOWN_ERROR);
|
|
260
|
+
|
|
261
|
+
const ERROR_FACTORIES = {
|
|
262
|
+
FS_NOT_AVAILABLE: (input?: BarekeyErrorInit) => new FsNotAvailableError(input),
|
|
263
|
+
NO_CONFIGURATION_PROVIDED: (input?: BarekeyErrorInit) => new NoConfigurationProvidedError(input),
|
|
264
|
+
INVALID_CONFIGURATION_PROVIDED: (input?: BarekeyErrorInit) =>
|
|
265
|
+
new InvalidConfigurationProvidedError(input),
|
|
266
|
+
NO_CREDENTIALS_PROVIDED: (input?: BarekeyErrorInit) => new NoCredentialsProvidedError(input),
|
|
267
|
+
INVALID_CREDENTIALS_PROVIDED: (input?: BarekeyErrorInit) =>
|
|
268
|
+
new InvalidCredentialsProvidedError(input),
|
|
269
|
+
INVALID_DYNAMIC_OPTIONS: (input?: BarekeyErrorInit) => new InvalidDynamicOptionsError(input),
|
|
270
|
+
REQUIREMENTS_VALIDATION_FAILED: (input?: BarekeyErrorInit) =>
|
|
271
|
+
new RequirementsValidationFailedError(input),
|
|
272
|
+
NETWORK_ERROR: (input?: BarekeyErrorInit) => new NetworkError(input),
|
|
273
|
+
COERCE_FAILED: (input?: BarekeyErrorInit) => new CoerceFailedError(input),
|
|
274
|
+
TEMPORAL_NOT_AVAILABLE: (input?: BarekeyErrorInit) => new TemporalNotAvailableError(input),
|
|
275
|
+
SDK_MODULE_NOT_FOUND: (input?: BarekeyErrorInit) => new SdkModuleNotFoundError(input),
|
|
276
|
+
TYPEGEN_UNSUPPORTED_SDK: (input?: BarekeyErrorInit) => new TypegenUnsupportedSdkError(input),
|
|
277
|
+
TYPEGEN_READ_FAILED: (input?: BarekeyErrorInit) => new TypegenReadFailedError(input),
|
|
278
|
+
TYPEGEN_WRITE_FAILED: (input?: BarekeyErrorInit) => new TypegenWriteFailedError(input),
|
|
279
|
+
UNAUTHORIZED: (input?: BarekeyErrorInit) => new UnauthorizedError(input),
|
|
280
|
+
INVALID_ORG_SCOPE: (input?: BarekeyErrorInit) => new InvalidOrgScopeError(input),
|
|
281
|
+
ORG_SCOPE_INVALID: (input?: BarekeyErrorInit) => new OrgScopeInvalidError(input),
|
|
282
|
+
INVALID_JSON: (input?: BarekeyErrorInit) => new InvalidJsonError(input),
|
|
283
|
+
INVALID_REQUEST: (input?: BarekeyErrorInit) => new InvalidRequestError(input),
|
|
284
|
+
VARIABLE_NOT_FOUND: (input?: BarekeyErrorInit) => new VariableNotFoundError(input),
|
|
285
|
+
EVALUATION_FAILED: (input?: BarekeyErrorInit) => new EvaluationFailedError(input),
|
|
286
|
+
USAGE_LIMIT_EXCEEDED: (input?: BarekeyErrorInit) => new UsageLimitExceededError(input),
|
|
287
|
+
BILLING_UNAVAILABLE: (input?: BarekeyErrorInit) => new BillingUnavailableError(input),
|
|
288
|
+
DEVICE_CODE_NOT_FOUND: (input?: BarekeyErrorInit) => new DeviceCodeNotFoundError(input),
|
|
289
|
+
DEVICE_CODE_EXPIRED: (input?: BarekeyErrorInit) => new DeviceCodeExpiredError(input),
|
|
290
|
+
USER_CODE_INVALID: (input?: BarekeyErrorInit) => new UserCodeInvalidError(input),
|
|
291
|
+
INVALID_REFRESH_TOKEN: (input?: BarekeyErrorInit) => new InvalidRefreshTokenError(input),
|
|
292
|
+
UNKNOWN_ERROR: (input?: BarekeyErrorInit) => new UnknownError(input),
|
|
293
|
+
} as const satisfies Record<BarekeyErrorCode, (input?: BarekeyErrorInit) => BarekeyError>;
|
|
294
|
+
|
|
295
|
+
export function isBarekeyErrorCode(value: string): value is BarekeyErrorCode {
|
|
296
|
+
return Object.hasOwn(ERROR_DESCRIPTORS, value);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export function normalizeErrorCode(value: string): BarekeyErrorCode {
|
|
300
|
+
const normalized = value.trim().toUpperCase();
|
|
301
|
+
return isBarekeyErrorCode(normalized) ? normalized : "UNKNOWN_ERROR";
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export function createBarekeyErrorFromCode(input: {
|
|
305
|
+
code: string;
|
|
306
|
+
message?: string;
|
|
307
|
+
requestId?: string | null;
|
|
308
|
+
status?: number | null;
|
|
309
|
+
cause?: unknown;
|
|
310
|
+
}): BarekeyError {
|
|
311
|
+
const code = normalizeErrorCode(input.code);
|
|
312
|
+
return ERROR_FACTORIES[code]({
|
|
313
|
+
message: input.message,
|
|
314
|
+
requestId: input.requestId,
|
|
315
|
+
status: input.status,
|
|
316
|
+
cause: input.cause,
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export function parseFloatOrThrow(value: string): number {
|
|
321
|
+
const normalized = value.trim();
|
|
322
|
+
if (normalized.length === 0) {
|
|
323
|
+
throw new CoerceFailedError({
|
|
324
|
+
message: `Barekey could not coerce "${value}" to a float.`,
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
const parsed = Number(normalized);
|
|
328
|
+
if (!Number.isFinite(parsed)) {
|
|
329
|
+
throw new CoerceFailedError({
|
|
330
|
+
message: `Barekey could not coerce "${value}" to a float.`,
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
return parsed;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export function parseBigIntOrThrow(value: string): bigint {
|
|
337
|
+
const normalized = value.trim();
|
|
338
|
+
if (normalized.length === 0 || !/^-?(0|[1-9]\d*)$/.test(normalized)) {
|
|
339
|
+
throw new CoerceFailedError({
|
|
340
|
+
message: `Barekey could not coerce "${value}" to an int64.`,
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
let parsed: bigint;
|
|
345
|
+
try {
|
|
346
|
+
parsed = BigInt(normalized);
|
|
347
|
+
} catch (error: unknown) {
|
|
348
|
+
throw new CoerceFailedError({
|
|
349
|
+
message: `Barekey could not coerce "${value}" to an int64.`,
|
|
350
|
+
cause: error,
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (parsed < INT64_MIN || parsed > INT64_MAX) {
|
|
355
|
+
throw new CoerceFailedError({
|
|
356
|
+
message: `Barekey could not coerce "${value}" to an int64.`,
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return parsed;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export function parseBooleanOrThrow(value: string): boolean {
|
|
364
|
+
const normalized = value.trim().toLowerCase();
|
|
365
|
+
if (normalized === "true" || normalized === "1" || normalized === "yes") {
|
|
366
|
+
return true;
|
|
367
|
+
}
|
|
368
|
+
if (normalized === "false" || normalized === "0" || normalized === "no") {
|
|
369
|
+
return false;
|
|
370
|
+
}
|
|
371
|
+
throw new CoerceFailedError({
|
|
372
|
+
message: `Barekey could not coerce "${value}" to a boolean.`,
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
export function parseJsonOrThrow<TJson = unknown>(value: string): TJson {
|
|
377
|
+
try {
|
|
378
|
+
return JSON.parse(value) as TJson;
|
|
379
|
+
} catch (error: unknown) {
|
|
380
|
+
throw new CoerceFailedError({
|
|
381
|
+
message: "Barekey could not coerce the resolved value to JSON.",
|
|
382
|
+
cause: error,
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export function parseDateOrThrow(value: string): Date {
|
|
388
|
+
const parsed = new Date(value);
|
|
389
|
+
if (Number.isNaN(parsed.getTime())) {
|
|
390
|
+
throw new CoerceFailedError({
|
|
391
|
+
message: `Barekey could not coerce "${value}" to a Date.`,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
return parsed;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export function parseTemporalInstantOrThrow(value: string): BarekeyTemporalInstant {
|
|
398
|
+
const temporalNamespace = (
|
|
399
|
+
globalThis as typeof globalThis & {
|
|
400
|
+
Temporal?: {
|
|
401
|
+
Instant?: {
|
|
402
|
+
from(value: string): BarekeyTemporalInstant;
|
|
403
|
+
};
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
).Temporal;
|
|
407
|
+
|
|
408
|
+
if (!temporalNamespace?.Instant) {
|
|
409
|
+
throw new TemporalNotAvailableError();
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
try {
|
|
413
|
+
return temporalNamespace.Instant.from(value);
|
|
414
|
+
} catch (error: unknown) {
|
|
415
|
+
throw new CoerceFailedError({
|
|
416
|
+
message: `Barekey could not coerce "${value}" to a Temporal.Instant.`,
|
|
417
|
+
cause: error,
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
}
|
package/src/handle.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {
|
|
2
|
+
coerceEvaluatedValue,
|
|
3
|
+
parseDeclaredValue,
|
|
4
|
+
} from "./internal/evaluate.js";
|
|
5
|
+
import type { BarekeyCoerceTarget, BarekeyEvaluatedValue } from "./types.js";
|
|
6
|
+
|
|
7
|
+
type BarekeyCoercibleEnvMarker = {
|
|
8
|
+
readonly __barekey?: {
|
|
9
|
+
readonly mode: "ab";
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export class BarekeyEnvHandle<TValue = unknown> implements PromiseLike<TValue> {
|
|
14
|
+
private readonly resolveEvaluatedValue: () => Promise<BarekeyEvaluatedValue>;
|
|
15
|
+
private readonly transform: (resolved: BarekeyEvaluatedValue) => Promise<TValue>;
|
|
16
|
+
private evaluatedValuePromise: Promise<BarekeyEvaluatedValue> | null = null;
|
|
17
|
+
|
|
18
|
+
constructor(
|
|
19
|
+
resolveEvaluatedValue: () => Promise<BarekeyEvaluatedValue>,
|
|
20
|
+
transform?: (resolved: BarekeyEvaluatedValue) => Promise<TValue>,
|
|
21
|
+
) {
|
|
22
|
+
this.resolveEvaluatedValue = resolveEvaluatedValue;
|
|
23
|
+
this.transform =
|
|
24
|
+
transform ??
|
|
25
|
+
(async (resolved) => parseDeclaredValue(resolved.value, resolved.declaredType) as TValue);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private async resolveValue(): Promise<TValue> {
|
|
29
|
+
return await this.transform(await this.getEvaluatedValue());
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
private async getEvaluatedValue(): Promise<BarekeyEvaluatedValue> {
|
|
33
|
+
if (this.evaluatedValuePromise === null) {
|
|
34
|
+
this.evaluatedValuePromise = this.resolveEvaluatedValue();
|
|
35
|
+
}
|
|
36
|
+
return await this.evaluatedValuePromise;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
coerce<TCoerced = unknown>(
|
|
40
|
+
this: [TValue] extends [BarekeyCoercibleEnvMarker] ? BarekeyEnvHandle<TValue> : never,
|
|
41
|
+
target: BarekeyCoerceTarget,
|
|
42
|
+
): BarekeyEnvHandle<TCoerced> {
|
|
43
|
+
return new BarekeyEnvHandle<TCoerced>(
|
|
44
|
+
async () => await this.getEvaluatedValue(),
|
|
45
|
+
async (resolved) => {
|
|
46
|
+
return coerceEvaluatedValue(resolved, target) as TCoerced;
|
|
47
|
+
},
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
then<TResult1 = TValue, TResult2 = never>(
|
|
52
|
+
onfulfilled?: ((value: TValue) => TResult1 | PromiseLike<TResult1>) | null,
|
|
53
|
+
onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,
|
|
54
|
+
): Promise<TResult1 | TResult2> {
|
|
55
|
+
return this.resolveValue().then(onfulfilled ?? undefined, onrejected ?? undefined);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
catch<TResult = never>(
|
|
59
|
+
onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null,
|
|
60
|
+
): Promise<TValue | TResult> {
|
|
61
|
+
return this.resolveValue().catch(onrejected ?? undefined);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
finally(onfinally?: (() => void) | null): Promise<TValue> {
|
|
65
|
+
return this.resolveValue().finally(onfinally ?? undefined);
|
|
66
|
+
}
|
|
67
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export { BarekeyClient } from "./client.js";
|
|
2
|
+
export { BarekeyEnvHandle } from "./handle.js";
|
|
3
|
+
export {
|
|
4
|
+
BarekeyError,
|
|
5
|
+
BillingUnavailableError,
|
|
6
|
+
CoerceFailedError,
|
|
7
|
+
DeviceCodeExpiredError,
|
|
8
|
+
DeviceCodeNotFoundError,
|
|
9
|
+
EvaluationFailedError,
|
|
10
|
+
FsNotAvailableError,
|
|
11
|
+
InvalidConfigurationProvidedError,
|
|
12
|
+
InvalidCredentialsProvidedError,
|
|
13
|
+
InvalidDynamicOptionsError,
|
|
14
|
+
InvalidJsonError,
|
|
15
|
+
InvalidOrgScopeError,
|
|
16
|
+
InvalidRefreshTokenError,
|
|
17
|
+
InvalidRequestError,
|
|
18
|
+
NetworkError,
|
|
19
|
+
NoConfigurationProvidedError,
|
|
20
|
+
NoCredentialsProvidedError,
|
|
21
|
+
OrgScopeInvalidError,
|
|
22
|
+
RequirementsValidationFailedError,
|
|
23
|
+
SdkModuleNotFoundError,
|
|
24
|
+
TemporalNotAvailableError,
|
|
25
|
+
TypegenReadFailedError,
|
|
26
|
+
TypegenUnsupportedSdkError,
|
|
27
|
+
TypegenWriteFailedError,
|
|
28
|
+
UnauthorizedError,
|
|
29
|
+
UnknownError,
|
|
30
|
+
UsageLimitExceededError,
|
|
31
|
+
UserCodeInvalidError,
|
|
32
|
+
VariableNotFoundError,
|
|
33
|
+
createBarekeyErrorFromCode,
|
|
34
|
+
docsUrlForErrorCode,
|
|
35
|
+
formatBarekeyErrorMessage,
|
|
36
|
+
isBarekeyErrorCode,
|
|
37
|
+
normalizeErrorCode,
|
|
38
|
+
} from "./errors.js";
|
|
39
|
+
|
|
40
|
+
export type {
|
|
41
|
+
AB,
|
|
42
|
+
BarekeyClientOptions,
|
|
43
|
+
BarekeyCoerceTarget,
|
|
44
|
+
BarekeyDeclaredType,
|
|
45
|
+
BarekeyErrorCode,
|
|
46
|
+
BarekeyGeneratedTypeMap,
|
|
47
|
+
BarekeyGetOptions,
|
|
48
|
+
BarekeyJsonConfig,
|
|
49
|
+
BarekeyKnownKey,
|
|
50
|
+
BarekeyResolvedKind,
|
|
51
|
+
BarekeyRolloutMilestone,
|
|
52
|
+
BarekeyStandardSchemaV1,
|
|
53
|
+
BarekeyTemporalInstant,
|
|
54
|
+
BarekeyTemporalInstantLike,
|
|
55
|
+
BarekeyTtlInput,
|
|
56
|
+
BarekeyTypegenResult,
|
|
57
|
+
Env,
|
|
58
|
+
Linear,
|
|
59
|
+
Secret,
|
|
60
|
+
} from "./types.js";
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
type MemoryCacheRecord<TValue> = {
|
|
2
|
+
value: TValue;
|
|
3
|
+
storedAtMs: number;
|
|
4
|
+
expiresAtMs: number | null;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export class MemoryCache<TValue> {
|
|
8
|
+
private readonly records = new Map<string, MemoryCacheRecord<TValue>>();
|
|
9
|
+
|
|
10
|
+
getRecord(key: string): MemoryCacheRecord<TValue> | null {
|
|
11
|
+
const record = this.records.get(key) ?? null;
|
|
12
|
+
if (record === null) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
if (record.expiresAtMs !== null && record.expiresAtMs <= Date.now()) {
|
|
16
|
+
this.records.delete(key);
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
return record;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get(key: string): TValue | null {
|
|
23
|
+
return this.getRecord(key)?.value ?? null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
set(key: string, value: TValue, expiresAtMs: number | null = null): void {
|
|
27
|
+
this.records.set(key, {
|
|
28
|
+
value,
|
|
29
|
+
storedAtMs: Date.now(),
|
|
30
|
+
expiresAtMs,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|