@barekey/sdk 0.1.2 → 0.2.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.
Files changed (50) hide show
  1. package/README.md +9 -3
  2. package/dist/client.d.ts +2 -3
  3. package/dist/client.d.ts.map +1 -1
  4. package/dist/index.d.ts +3 -1
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +1 -0
  7. package/dist/internal/node-runtime.d.ts +2 -1
  8. package/dist/internal/node-runtime.d.ts.map +1 -1
  9. package/dist/internal/node-runtime.js +7 -4
  10. package/dist/internal/public-runtime.d.ts +14 -0
  11. package/dist/internal/public-runtime.d.ts.map +1 -0
  12. package/dist/internal/public-runtime.js +78 -0
  13. package/dist/internal/typegen.d.ts +5 -0
  14. package/dist/internal/typegen.d.ts.map +1 -1
  15. package/dist/internal/typegen.js +44 -13
  16. package/dist/key-types.typecheck.d.ts +13 -0
  17. package/dist/key-types.typecheck.d.ts.map +1 -0
  18. package/dist/key-types.typecheck.js +5 -0
  19. package/dist/public-client.d.ts +35 -0
  20. package/dist/public-client.d.ts.map +1 -0
  21. package/dist/public-client.js +150 -0
  22. package/dist/public-types.d.ts +27 -0
  23. package/dist/public-types.d.ts.map +1 -0
  24. package/dist/public-types.js +1 -0
  25. package/dist/public.d.ts +6 -0
  26. package/dist/public.d.ts.map +1 -0
  27. package/dist/public.js +3 -0
  28. package/dist/server.d.ts +5 -0
  29. package/dist/server.d.ts.map +1 -0
  30. package/dist/server.js +3 -0
  31. package/dist/types.d.ts +6 -1
  32. package/dist/types.d.ts.map +1 -1
  33. package/generated.public.d.ts +8 -0
  34. package/generated.server.d.ts +8 -0
  35. package/index.d.ts +0 -1
  36. package/package.json +18 -3
  37. package/public.d.ts +2 -0
  38. package/server.d.ts +2 -0
  39. package/src/client.ts +3 -3
  40. package/src/index.ts +10 -0
  41. package/src/internal/node-runtime.ts +9 -5
  42. package/src/internal/public-runtime.ts +123 -0
  43. package/src/internal/typegen.ts +68 -13
  44. package/src/key-types.typecheck.ts +68 -0
  45. package/src/public-client.ts +231 -0
  46. package/src/public-types.ts +63 -0
  47. package/src/public.ts +71 -0
  48. package/src/server.ts +62 -0
  49. package/src/types.ts +8 -1
  50. package/generated.d.ts +0 -16
@@ -0,0 +1,231 @@
1
+ import { VariableNotFoundError } from "./errors.js";
2
+ import { BarekeyEnvHandle } from "./handle.js";
3
+ import {
4
+ evaluateDefinition,
5
+ parseDeclaredValue,
6
+ validateDynamicOptions,
7
+ } from "./internal/evaluate.js";
8
+ import { postJson } from "./internal/http.js";
9
+ import { MemoryCache } from "./internal/cache.js";
10
+ import { validateRequirements } from "./internal/requirements.js";
11
+ import {
12
+ resolvePublicRuntimeContext,
13
+ type BarekeyPublicRuntimeContext,
14
+ } from "./internal/public-runtime.js";
15
+ import { resolveTtlMilliseconds } from "./internal/ttl.js";
16
+ import type {
17
+ BarekeyPublicGeneratedTypeMap,
18
+ BarekeyPublicKey,
19
+ BarekeyPublicKnownKey,
20
+ PublicBarekeyClientOptions,
21
+ } from "./public-types.js";
22
+ import type {
23
+ BarekeyEvaluatedValue,
24
+ BarekeyGetOptions,
25
+ BarekeyJsonConfig,
26
+ BarekeyVariableDefinition,
27
+ } from "./types.js";
28
+
29
+ type DefinitionsResponse = {
30
+ definitions: Array<BarekeyVariableDefinition>;
31
+ };
32
+
33
+ function createDefaultFetch(): typeof globalThis.fetch {
34
+ if (typeof globalThis.fetch === "function") {
35
+ return globalThis.fetch.bind(globalThis);
36
+ }
37
+
38
+ return (async () => {
39
+ throw new Error("fetch is not available in this runtime.");
40
+ }) as typeof globalThis.fetch;
41
+ }
42
+
43
+ export class PublicBarekeyClient {
44
+ private readonly options: PublicBarekeyClientOptions;
45
+ private readonly fetchFn: typeof globalThis.fetch;
46
+ private readonly definitionCache = new MemoryCache<BarekeyVariableDefinition>();
47
+ private readonly evaluationCache = new MemoryCache<BarekeyEvaluatedValue>();
48
+ private runtimeContextPromise: Promise<BarekeyPublicRuntimeContext> | null = null;
49
+ private requirementsPromise: Promise<void> | null = null;
50
+
51
+ constructor();
52
+ constructor(options: {
53
+ organization: string;
54
+ project: string;
55
+ environment: string;
56
+ requirements?: PublicBarekeyClientOptions["requirements"];
57
+ baseUrl?: string;
58
+ });
59
+ constructor(options: {
60
+ json: BarekeyJsonConfig;
61
+ requirements?: PublicBarekeyClientOptions["requirements"];
62
+ baseUrl?: string;
63
+ });
64
+ constructor(options: PublicBarekeyClientOptions = {}) {
65
+ this.options = options;
66
+ this.fetchFn = createDefaultFetch();
67
+ }
68
+
69
+ get<TKey extends BarekeyPublicKey>(
70
+ name: TKey,
71
+ options?: BarekeyGetOptions,
72
+ ): BarekeyEnvHandle<
73
+ TKey extends BarekeyPublicKnownKey ? BarekeyPublicGeneratedTypeMap[TKey] : unknown
74
+ >;
75
+ get(name: string, options?: BarekeyGetOptions): BarekeyEnvHandle<unknown> {
76
+ return new BarekeyEnvHandle(
77
+ async () => await this.resolveEvaluatedValue(name, options),
78
+ );
79
+ }
80
+
81
+ private async getRuntimeContext(): Promise<BarekeyPublicRuntimeContext> {
82
+ if (this.runtimeContextPromise === null) {
83
+ const runtimeContextPromise = resolvePublicRuntimeContext(this.options);
84
+ runtimeContextPromise.catch(() => {
85
+ if (this.runtimeContextPromise === runtimeContextPromise) {
86
+ this.runtimeContextPromise = null;
87
+ }
88
+ });
89
+ this.runtimeContextPromise = runtimeContextPromise;
90
+ }
91
+ return await this.runtimeContextPromise;
92
+ }
93
+
94
+ private buildDefinitionCacheKey(context: BarekeyPublicRuntimeContext, name: string): string {
95
+ return [context.organization, context.project, context.environment, name].join("|");
96
+ }
97
+
98
+ private buildEvaluationCacheKey(
99
+ context: BarekeyPublicRuntimeContext,
100
+ name: string,
101
+ options?: BarekeyGetOptions,
102
+ ): string {
103
+ return [
104
+ context.organization,
105
+ context.project,
106
+ context.environment,
107
+ name,
108
+ options?.seed ?? "",
109
+ options?.key ?? "",
110
+ ].join("|");
111
+ }
112
+
113
+ private async fetchDefinitions(names?: Array<string>): Promise<Array<BarekeyVariableDefinition>> {
114
+ const context = await this.getRuntimeContext();
115
+ const response = await postJson<DefinitionsResponse>({
116
+ fetchFn: this.fetchFn,
117
+ baseUrl: context.baseUrl,
118
+ path: "/v1/public/env/definitions",
119
+ payload: {
120
+ orgSlug: context.organization,
121
+ projectSlug: context.project,
122
+ stageSlug: context.environment,
123
+ ...(names === undefined ? {} : { names }),
124
+ },
125
+ });
126
+
127
+ for (const definition of response.definitions) {
128
+ this.definitionCache.set(this.buildDefinitionCacheKey(context, definition.name), definition);
129
+ }
130
+
131
+ return response.definitions;
132
+ }
133
+
134
+ private async ensureRequirementsValidated(): Promise<void> {
135
+ const context = await this.getRuntimeContext();
136
+ const requirements = context.requirements;
137
+ if (requirements === undefined) {
138
+ return;
139
+ }
140
+
141
+ if (this.requirementsPromise === null) {
142
+ const requirementsPromise = (async () => {
143
+ const definitions = await this.fetchDefinitions();
144
+ const values: Record<string, unknown> = {};
145
+ for (const definition of definitions) {
146
+ const evaluated = await evaluateDefinition(definition);
147
+ values[definition.name] = parseDeclaredValue(evaluated.value, evaluated.declaredType);
148
+ }
149
+ await validateRequirements(requirements, values);
150
+ })();
151
+ requirementsPromise.catch(() => {
152
+ if (this.requirementsPromise === requirementsPromise) {
153
+ this.requirementsPromise = null;
154
+ }
155
+ });
156
+ this.requirementsPromise = requirementsPromise;
157
+ }
158
+
159
+ await this.requirementsPromise;
160
+ }
161
+
162
+ private async getStaticDefinition(name: string): Promise<BarekeyVariableDefinition> {
163
+ await this.ensureRequirementsValidated();
164
+ const context = await this.getRuntimeContext();
165
+ const cacheKey = this.buildDefinitionCacheKey(context, name);
166
+ const cached = this.definitionCache.get(cacheKey);
167
+ if (cached !== null) {
168
+ return cached;
169
+ }
170
+
171
+ const definitions = await this.fetchDefinitions([name]);
172
+ const resolved = definitions[0];
173
+ if (resolved === undefined) {
174
+ throw new VariableNotFoundError();
175
+ }
176
+ return resolved;
177
+ }
178
+
179
+ private async resolveStaticValue(
180
+ name: string,
181
+ options?: BarekeyGetOptions,
182
+ ): Promise<BarekeyEvaluatedValue> {
183
+ const definition = await this.getStaticDefinition(name);
184
+ return await evaluateDefinition(definition, options);
185
+ }
186
+
187
+ private async resolveDynamicValue(
188
+ name: string,
189
+ options?: BarekeyGetOptions,
190
+ ): Promise<BarekeyEvaluatedValue> {
191
+ await this.ensureRequirementsValidated();
192
+ const context = await this.getRuntimeContext();
193
+ const cacheKey = this.buildEvaluationCacheKey(context, name, options);
194
+ const dynamic = options?.dynamic;
195
+ const dynamicTtlMs =
196
+ dynamic !== undefined && dynamic !== true
197
+ ? resolveTtlMilliseconds(dynamic.ttl, "dynamic.ttl")
198
+ : null;
199
+
200
+ if (dynamic !== true) {
201
+ const cached = this.evaluationCache.getRecord(cacheKey);
202
+ if (cached !== null && dynamicTtlMs !== null && Date.now() - cached.storedAtMs <= dynamicTtlMs) {
203
+ return cached.value;
204
+ }
205
+ }
206
+
207
+ const freshDefinitions = await this.fetchDefinitions([name]);
208
+ const freshDefinition = freshDefinitions[0];
209
+ if (freshDefinition === undefined) {
210
+ throw new VariableNotFoundError();
211
+ }
212
+ const resolved = await evaluateDefinition(freshDefinition, options);
213
+
214
+ if (dynamicTtlMs !== null) {
215
+ this.evaluationCache.set(cacheKey, resolved);
216
+ }
217
+
218
+ return resolved;
219
+ }
220
+
221
+ private async resolveEvaluatedValue(
222
+ name: string,
223
+ options?: BarekeyGetOptions,
224
+ ): Promise<BarekeyEvaluatedValue> {
225
+ validateDynamicOptions(options);
226
+ if (options?.dynamic === undefined) {
227
+ return await this.resolveStaticValue(name, options);
228
+ }
229
+ return await this.resolveDynamicValue(name, options);
230
+ }
231
+ }
@@ -0,0 +1,63 @@
1
+ export type {
2
+ AB,
3
+ BarekeyCoerceTarget,
4
+ BarekeyDeclaredType,
5
+ BarekeyDecision,
6
+ BarekeyErrorCode,
7
+ BarekeyEvaluatedValue,
8
+ BarekeyGetOptions,
9
+ BarekeyJsonConfig,
10
+ BarekeyResolvedKind,
11
+ BarekeyRolloutFunction,
12
+ BarekeyRolloutMatchedRule,
13
+ BarekeyRolloutMilestone,
14
+ BarekeyStandardSchemaPathSegment,
15
+ BarekeyStandardSchemaResult,
16
+ BarekeyStandardSchemaV1,
17
+ BarekeyTemporalInstant,
18
+ BarekeyTemporalInstantLike,
19
+ BarekeyTtlInput,
20
+ BarekeyTypegenResult,
21
+ BarekeyVariableDefinition,
22
+ EaseInOut,
23
+ Env,
24
+ Linear,
25
+ Secret,
26
+ Step,
27
+ } from "./types.js";
28
+ import type {
29
+ BarekeyJsonConfig,
30
+ BarekeyLiteralString,
31
+ BarekeyStandardSchemaV1,
32
+ } from "./types.js";
33
+
34
+ export interface BarekeyPublicGeneratedTypeMap {}
35
+
36
+ export type BarekeyPublicKnownKey = Extract<keyof BarekeyPublicGeneratedTypeMap, string>;
37
+
38
+ export type BarekeyPublicKey = BarekeyPublicKnownKey | BarekeyLiteralString;
39
+
40
+ type BarekeyPublicBaseClientOptions = {
41
+ requirements?: BarekeyStandardSchemaV1;
42
+ baseUrl?: string;
43
+ };
44
+
45
+ export type PublicBarekeyClientOptions =
46
+ | (BarekeyPublicBaseClientOptions & {
47
+ organization: string;
48
+ project: string;
49
+ environment: string;
50
+ json?: never;
51
+ })
52
+ | (BarekeyPublicBaseClientOptions & {
53
+ json: BarekeyJsonConfig;
54
+ organization?: never;
55
+ project?: never;
56
+ environment?: never;
57
+ })
58
+ | (BarekeyPublicBaseClientOptions & {
59
+ organization?: never;
60
+ project?: never;
61
+ environment?: never;
62
+ json?: never;
63
+ });
package/src/public.ts ADDED
@@ -0,0 +1,71 @@
1
+ export { PublicBarekeyClient } from "./public-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
+ BarekeyCoerceTarget,
43
+ BarekeyDeclaredType,
44
+ BarekeyDecision,
45
+ BarekeyErrorCode,
46
+ BarekeyEvaluatedValue,
47
+ BarekeyGetOptions,
48
+ BarekeyJsonConfig,
49
+ BarekeyLiteralString,
50
+ BarekeyResolvedKind,
51
+ BarekeyRolloutFunction,
52
+ BarekeyRolloutMatchedRule,
53
+ BarekeyRolloutMilestone,
54
+ BarekeyStandardSchemaV1,
55
+ BarekeyTemporalInstant,
56
+ BarekeyTemporalInstantLike,
57
+ BarekeyTtlInput,
58
+ BarekeyVariableDefinition,
59
+ EaseInOut,
60
+ Env,
61
+ Linear,
62
+ Secret,
63
+ Step,
64
+ } from "./types.js";
65
+
66
+ export type {
67
+ BarekeyPublicGeneratedTypeMap,
68
+ BarekeyPublicKey,
69
+ BarekeyPublicKnownKey,
70
+ PublicBarekeyClientOptions,
71
+ } from "./public-types.js";
package/src/server.ts ADDED
@@ -0,0 +1,62 @@
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
+ BarekeyKey,
50
+ BarekeyLiteralString,
51
+ BarekeyKnownKey,
52
+ BarekeyResolvedKind,
53
+ BarekeyRolloutMilestone,
54
+ BarekeyStandardSchemaV1,
55
+ BarekeyTemporalInstant,
56
+ BarekeyTemporalInstantLike,
57
+ BarekeyTtlInput,
58
+ BarekeyTypegenResult,
59
+ Env,
60
+ Linear,
61
+ Secret,
62
+ } from "./types.js";
package/src/types.ts CHANGED
@@ -45,10 +45,11 @@ export type EaseInOut<
45
45
  readonly milestones: TMilestones;
46
46
  };
47
47
 
48
- export type Env<TMode, TValue, TFunction = never> = TValue & {
48
+ export type Env<TMode, TValue, TFunction = never, V = never> = TValue & {
49
49
  readonly __barekey?: {
50
50
  readonly mode: TMode;
51
51
  readonly function: TFunction;
52
+ readonly visibility: V;
52
53
  };
53
54
  };
54
55
 
@@ -58,6 +59,10 @@ export interface BarekeyGeneratedTypeMap {}
58
59
 
59
60
  export type BarekeyKnownKey = Extract<keyof BarekeyGeneratedTypeMap, string>;
60
61
 
62
+ export type BarekeyLiteralString = string & {};
63
+
64
+ export type BarekeyKey = BarekeyKnownKey | BarekeyLiteralString;
65
+
61
66
  export type BarekeyRolloutMilestone = {
62
67
  at: string;
63
68
  percentage: number;
@@ -160,6 +165,8 @@ type BarekeyBaseClientOptions = {
160
165
  export type BarekeyTypegenResult = {
161
166
  written: boolean;
162
167
  path: string;
168
+ serverPath: string;
169
+ publicPath: string;
163
170
  manifestVersion: string;
164
171
  };
165
172
 
package/generated.d.ts DELETED
@@ -1,16 +0,0 @@
1
- /* eslint-disable */
2
- /* This file is generated by barekey typegen. */
3
- /* barekey-manifest-version: REnQvc_83Zn-Xs-tvl2fA375JUy3Jp1IOIoYFB8y-74 */
4
-
5
- import type { AB, Env, Linear, Secret } from "./dist/types.js";
6
-
7
- declare module "./dist/types.js" {
8
- interface BarekeyGeneratedTypeMap {
9
- "CODEX_TEST_SECRET": Env<Secret, string>;
10
- "DATABASE_URL": Env<Secret, string>;
11
- "foo": Env<Secret, string>;
12
- "JSON_DEEP": Env<Secret, { bar: boolean; foo: string; nested: { count: number; flags: Array<boolean>; meta: { label: string; }; }; }>;
13
- }
14
- }
15
-
16
- export {};