@pokit/op 0.0.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Daniel Grant
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,3 @@
1
+ export declare const opInstalled: import("@pokit/core").CheckConfig;
2
+ export declare const opAuthenticated: import("@pokit/core").CheckConfig;
3
+ //# sourceMappingURL=checks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checks.d.ts","sourceRoot":"","sources":["../src/checks.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,WAAW,mCAWtB,CAAC;AAEH,eAAO,MAAM,eAAe,mCAQ1B,CAAC"}
package/dist/checks.js ADDED
@@ -0,0 +1,21 @@
1
+ import { defineCheck } from '@pokit/core';
2
+ import { isInstalled, isAuthenticated, getAuthErrorMessage } from './op';
3
+ export const opInstalled = defineCheck({
4
+ label: '1Password CLI installed',
5
+ check: async () => {
6
+ const installed = await isInstalled();
7
+ if (!installed) {
8
+ throw new Error('1Password CLI is not installed. ' +
9
+ 'Install from: https://developer.1password.com/docs/cli/get-started/');
10
+ }
11
+ },
12
+ });
13
+ export const opAuthenticated = defineCheck({
14
+ label: '1Password authenticated',
15
+ check: async () => {
16
+ const authenticated = await isAuthenticated();
17
+ if (!authenticated) {
18
+ throw new Error(getAuthErrorMessage());
19
+ }
20
+ },
21
+ });
@@ -0,0 +1,16 @@
1
+ export { defineOpVault, parseOpRef } from './vault';
2
+ export type { OpVault, OpVaultRef, InferOpVaultKeys } from './vault';
3
+ export { defineOpResolver } from './resolver';
4
+ export type { OpResolverConfig } from './resolver';
5
+ export * as opUtils from './op';
6
+ export type { OpItem } from './op';
7
+ /**
8
+ * Type exports for individual utility functions.
9
+ * These enable consumers to reference specific function signatures
10
+ * without accessing them through the opUtils namespace.
11
+ */
12
+ export type IsInstalledFn = typeof import('./op').isInstalled;
13
+ export type IsAuthenticatedFn = typeof import('./op').isAuthenticated;
14
+ export type GetFieldFn = typeof import('./op').getField;
15
+ export { opInstalled, opAuthenticated } from './checks';
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACpD,YAAY,EAAE,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAErE,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAC9C,YAAY,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAMnD,OAAO,KAAK,OAAO,MAAM,MAAM,CAAC;AAEhC,YAAY,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAEnC;;;;GAIG;AACH,MAAM,MAAM,aAAa,GAAG,cAAc,MAAM,EAAE,WAAW,CAAC;AAC9D,MAAM,MAAM,iBAAiB,GAAG,cAAc,MAAM,EAAE,eAAe,CAAC;AACtE,MAAM,MAAM,UAAU,GAAG,cAAc,MAAM,EAAE,QAAQ,CAAC;AAMxD,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,10 @@
1
+ export { defineOpVault, parseOpRef } from './vault';
2
+ export { defineOpResolver } from './resolver';
3
+ // =============================================================================
4
+ // 1Password CLI utilities
5
+ // =============================================================================
6
+ export * as opUtils from './op';
7
+ // =============================================================================
8
+ // 1Password CLI checks
9
+ // =============================================================================
10
+ export { opInstalled, opAuthenticated } from './checks';
package/dist/op.d.ts ADDED
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Check if 1Password CLI is installed
3
+ */
4
+ export declare function isInstalled(): Promise<boolean>;
5
+ /**
6
+ * Check if authenticated with 1Password CLI
7
+ * Works for both desktop (app/signin) and CI (OP_SERVICE_ACCOUNT_TOKEN)
8
+ */
9
+ export declare function isAuthenticated(): Promise<boolean>;
10
+ /**
11
+ * Get helpful error message for authentication failure
12
+ */
13
+ export declare function getAuthErrorMessage(): string;
14
+ /**
15
+ * Check if a vault exists
16
+ */
17
+ export declare function vaultExists(vault: string): Promise<boolean>;
18
+ /**
19
+ * Create a vault
20
+ */
21
+ export declare function createVault(vault: string): Promise<void>;
22
+ /**
23
+ * List all vaults
24
+ */
25
+ export declare function listVaults(): Promise<string[]>;
26
+ /**
27
+ * Item structure
28
+ */
29
+ export interface OpItem {
30
+ id: string;
31
+ title: string;
32
+ vault: {
33
+ id: string;
34
+ name: string;
35
+ };
36
+ fields: Record<string, string>;
37
+ }
38
+ /**
39
+ * Check if an item exists in a vault
40
+ */
41
+ export declare function itemExists(vault: string, item: string): Promise<boolean>;
42
+ /**
43
+ * Get a field value from a 1Password item
44
+ */
45
+ export declare function getField(vault: string, item: string, field: string): Promise<string | null>;
46
+ /**
47
+ * Get all fields from a 1Password item
48
+ */
49
+ export declare function getItem(vault: string, item: string): Promise<OpItem | null>;
50
+ /**
51
+ * Get multiple items from a vault in a single batch
52
+ * More efficient than calling getItem multiple times
53
+ */
54
+ export declare function getItemsBatch(vault: string, itemNames: string[]): Promise<Map<string, OpItem>>;
55
+ /**
56
+ * Set a field on a 1Password item, creating the item if it doesn't exist
57
+ */
58
+ export declare function setField(vault: string, item: string, field: string, value: string): Promise<void>;
59
+ /**
60
+ * Set multiple fields on a 1Password item in a single operation.
61
+ * Batches all field updates into a single CLI call for efficiency.
62
+ */
63
+ export declare function setFieldsBatch(vault: string, item: string, fields: Record<string, string>): Promise<void>;
64
+ /**
65
+ * Resolve a secret reference (op://vault/item/field)
66
+ */
67
+ export declare function resolveSecret(reference: string): Promise<string | null>;
68
+ //# sourceMappingURL=op.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"op.d.ts","sourceRoot":"","sources":["../src/op.ts"],"names":[],"mappings":"AA+BA;;GAEG;AACH,wBAAsB,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC,CAGpD;AAED;;;GAGG;AACH,wBAAsB,eAAe,IAAI,OAAO,CAAC,OAAO,CAAC,CAGxD;AAED;;GAEG;AACH,wBAAgB,mBAAmB,IAAI,MAAM,CAW5C;AAED;;GAEG;AACH,wBAAsB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAIjE;AAED;;GAEG;AACH,wBAAsB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAG9D;AAED;;GAEG;AACH,wBAAsB,UAAU,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC,CAIpD;AAED;;GAEG;AACH,MAAM,WAAW,MAAM;IACrB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IACpC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAChC;AAED;;GAEG;AACH,wBAAsB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAK9E;AAED;;GAEG;AACH,wBAAsB,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CASjG;AAED;;GAEG;AACH,wBAAsB,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CA6BjF;AAED;;;GAGG;AACH,wBAAsB,aAAa,CACjC,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,EAAE,GAClB,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAmB9B;AAED;;GAEG;AACH,wBAAsB,QAAQ,CAC5B,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,IAAI,CAAC,CAEf;AAED;;;GAGG;AACH,wBAAsB,cAAc,CAClC,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAC7B,OAAO,CAAC,IAAI,CAAC,CAqBf;AAED;;GAEG;AACH,wBAAsB,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAM7E"}
package/dist/op.js ADDED
@@ -0,0 +1,176 @@
1
+ import { $ } from 'bun';
2
+ // =============================================================================
3
+ // Input Validation
4
+ // =============================================================================
5
+ /**
6
+ * Valid characters for 1Password identifiers (vault, item, field names).
7
+ * Allows alphanumeric, spaces, dashes, underscores, and periods.
8
+ */
9
+ const VALID_IDENTIFIER_PATTERN = /^[a-zA-Z0-9 _.-]+$/;
10
+ /**
11
+ * Validate a 1Password identifier (vault name, item name, or field name).
12
+ * Throws if the identifier contains invalid characters.
13
+ */
14
+ function validateIdentifier(value, type) {
15
+ if (!value || value.trim().length === 0) {
16
+ throw new Error(`${type} name cannot be empty`);
17
+ }
18
+ if (!VALID_IDENTIFIER_PATTERN.test(value)) {
19
+ throw new Error(`Invalid ${type} name: "${value}". Only alphanumeric characters, spaces, dashes, underscores, and periods are allowed.`);
20
+ }
21
+ }
22
+ // =============================================================================
23
+ // Authentication
24
+ // =============================================================================
25
+ /**
26
+ * Check if 1Password CLI is installed
27
+ */
28
+ export async function isInstalled() {
29
+ const result = await $ `which op`.nothrow().quiet();
30
+ return result.exitCode === 0;
31
+ }
32
+ /**
33
+ * Check if authenticated with 1Password CLI
34
+ * Works for both desktop (app/signin) and CI (OP_SERVICE_ACCOUNT_TOKEN)
35
+ */
36
+ export async function isAuthenticated() {
37
+ const result = await $ `op whoami`.nothrow().quiet();
38
+ return result.exitCode === 0;
39
+ }
40
+ /**
41
+ * Get helpful error message for authentication failure
42
+ */
43
+ export function getAuthErrorMessage() {
44
+ if (process.env.OP_SERVICE_ACCOUNT_TOKEN) {
45
+ return ('1Password authentication failed. ' +
46
+ 'The OP_SERVICE_ACCOUNT_TOKEN may be invalid or expired.');
47
+ }
48
+ return ('1Password authentication failed. ' +
49
+ 'Either run `op signin` or ensure the 1Password app is running with CLI integration enabled.');
50
+ }
51
+ /**
52
+ * Check if a vault exists
53
+ */
54
+ export async function vaultExists(vault) {
55
+ validateIdentifier(vault, 'vault');
56
+ const result = await $ `op vault get ${vault} --format=json`.nothrow().quiet();
57
+ return result.exitCode === 0;
58
+ }
59
+ /**
60
+ * Create a vault
61
+ */
62
+ export async function createVault(vault) {
63
+ validateIdentifier(vault, 'vault');
64
+ await $ `op vault create ${vault}`.quiet();
65
+ }
66
+ /**
67
+ * List all vaults
68
+ */
69
+ export async function listVaults() {
70
+ const result = await $ `op vault list --format=json`.quiet();
71
+ const vaults = JSON.parse(result.text());
72
+ return vaults.map((v) => v.name);
73
+ }
74
+ /**
75
+ * Check if an item exists in a vault
76
+ */
77
+ export async function itemExists(vault, item) {
78
+ validateIdentifier(vault, 'vault');
79
+ validateIdentifier(item, 'item');
80
+ const result = await $ `op item get ${item} --vault=${vault} --format=json`.nothrow().quiet();
81
+ return result.exitCode === 0;
82
+ }
83
+ /**
84
+ * Get a field value from a 1Password item
85
+ */
86
+ export async function getField(vault, item, field) {
87
+ validateIdentifier(vault, 'vault');
88
+ validateIdentifier(item, 'item');
89
+ validateIdentifier(field, 'field');
90
+ const result = await $ `op read op://${vault}/${item}/${field}`.nothrow().quiet();
91
+ if (result.exitCode !== 0) {
92
+ return null;
93
+ }
94
+ return result.text().trim();
95
+ }
96
+ /**
97
+ * Get all fields from a 1Password item
98
+ */
99
+ export async function getItem(vault, item) {
100
+ validateIdentifier(vault, 'vault');
101
+ validateIdentifier(item, 'item');
102
+ const result = await $ `op item get ${item} --vault=${vault} --format=json`.nothrow().quiet();
103
+ if (result.exitCode !== 0) {
104
+ return null;
105
+ }
106
+ const data = JSON.parse(result.text());
107
+ const fields = {};
108
+ for (const f of data.fields) {
109
+ if (f.value) {
110
+ fields[f.label] = f.value;
111
+ }
112
+ }
113
+ return {
114
+ id: data.id,
115
+ title: data.title,
116
+ vault: data.vault,
117
+ fields,
118
+ };
119
+ }
120
+ /**
121
+ * Get multiple items from a vault in a single batch
122
+ * More efficient than calling getItem multiple times
123
+ */
124
+ export async function getItemsBatch(vault, itemNames) {
125
+ validateIdentifier(vault, 'vault');
126
+ for (const itemName of itemNames) {
127
+ validateIdentifier(itemName, 'item');
128
+ }
129
+ const results = new Map();
130
+ // Fetch items in parallel
131
+ await Promise.all(itemNames.map(async (itemName) => {
132
+ const item = await getItem(vault, itemName);
133
+ if (item) {
134
+ results.set(itemName, item);
135
+ }
136
+ }));
137
+ return results;
138
+ }
139
+ /**
140
+ * Set a field on a 1Password item, creating the item if it doesn't exist
141
+ */
142
+ export async function setField(vault, item, field, value) {
143
+ return setFieldsBatch(vault, item, { [field]: value });
144
+ }
145
+ /**
146
+ * Set multiple fields on a 1Password item in a single operation.
147
+ * Batches all field updates into a single CLI call for efficiency.
148
+ */
149
+ export async function setFieldsBatch(vault, item, fields) {
150
+ validateIdentifier(vault, 'vault');
151
+ validateIdentifier(item, 'item');
152
+ for (const fieldName of Object.keys(fields)) {
153
+ validateIdentifier(fieldName, 'field');
154
+ }
155
+ const exists = await itemExists(vault, item);
156
+ // Build field arguments for all fields
157
+ const fieldArgs = Object.entries(fields).map(([fieldName, fieldValue]) => `${fieldName}[concealed]=${fieldValue}`);
158
+ if (exists) {
159
+ // Update existing item - edit all fields in a single command
160
+ await $ `op item edit ${item} --vault=${vault} ${fieldArgs}`.quiet();
161
+ }
162
+ else {
163
+ // Create new item with all fields
164
+ await $ `op item create --category=login --vault=${vault} --title=${item} ${fieldArgs}`.quiet();
165
+ }
166
+ }
167
+ /**
168
+ * Resolve a secret reference (op://vault/item/field)
169
+ */
170
+ export async function resolveSecret(reference) {
171
+ const result = await $ `op read ${reference}`.nothrow().quiet();
172
+ if (result.exitCode !== 0) {
173
+ return null;
174
+ }
175
+ return result.text().trim();
176
+ }
@@ -0,0 +1,28 @@
1
+ import type { TypedEnvResolver } from '@pokit/core';
2
+ import { type OpVault, type InferOpVaultKeys } from './vault';
3
+ export type OpResolverConfig<TVault extends OpVault<any>, TEnvs extends string> = {
4
+ vault: TVault;
5
+ vaults: Record<TEnvs, string>;
6
+ };
7
+ /**
8
+ * Define a 1Password resolver that fetches and writes secrets to 1Password vaults.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * const vault = defineOpVault({
13
+ * POSTGRES_URL: 'supabase:SUPABASE_SESSION_DSN',
14
+ * SUPABASE_URL: 'supabase:SUPABASE_URL',
15
+ * });
16
+ *
17
+ * const opResolver = defineOpResolver({
18
+ * vault,
19
+ * vaults: {
20
+ * dev: 'my-app-secrets-dev',
21
+ * staging: 'my-app-secrets-staging',
22
+ * prod: 'my-app-secrets-prod',
23
+ * },
24
+ * });
25
+ * ```
26
+ */
27
+ export declare function defineOpResolver<TVault extends OpVault<any>, const TEnvs extends string>(config: OpResolverConfig<TVault, TEnvs>): TypedEnvResolver<InferOpVaultKeys<TVault> & string>;
28
+ //# sourceMappingURL=resolver.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolver.d.ts","sourceRoot":"","sources":["../src/resolver.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAkB,MAAM,aAAa,CAAC;AACpE,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,gBAAgB,EAAc,MAAM,SAAS,CAAC;AAS1E,MAAM,MAAM,gBAAgB,CAAC,MAAM,SAAS,OAAO,CAAC,GAAG,CAAC,EAAE,KAAK,SAAS,MAAM,IAAI;IAChF,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;CAC/B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,SAAS,OAAO,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,KAAK,SAAS,MAAM,EACtF,MAAM,EAAE,gBAAgB,CAAC,MAAM,EAAE,KAAK,CAAC,GACtC,gBAAgB,CAAC,gBAAgB,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,CAwGrD"}
@@ -0,0 +1,105 @@
1
+ import { z } from 'zod';
2
+ import { parseOpRef } from './vault';
3
+ import * as op from './op';
4
+ /**
5
+ * Define a 1Password resolver that fetches and writes secrets to 1Password vaults.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const vault = defineOpVault({
10
+ * POSTGRES_URL: 'supabase:SUPABASE_SESSION_DSN',
11
+ * SUPABASE_URL: 'supabase:SUPABASE_URL',
12
+ * });
13
+ *
14
+ * const opResolver = defineOpResolver({
15
+ * vault,
16
+ * vaults: {
17
+ * dev: 'my-app-secrets-dev',
18
+ * staging: 'my-app-secrets-staging',
19
+ * prod: 'my-app-secrets-prod',
20
+ * },
21
+ * });
22
+ * ```
23
+ */
24
+ export function defineOpResolver(config) {
25
+ const envValues = Object.keys(config.vaults);
26
+ const availableVars = Object.keys(config.vault.secrets);
27
+ return {
28
+ requiredContext: z.object({
29
+ env: z.enum(envValues),
30
+ }),
31
+ availableVars,
32
+ resolve: async (keys, context) => {
33
+ const ctx = z.object({ env: z.enum(envValues) }).parse(context);
34
+ const vaultName = config.vaults[ctx.env];
35
+ if (!vaultName) {
36
+ throw new Error(`No vault configured for environment: ${ctx.env}`);
37
+ }
38
+ // Build mapping of keys to their 1Password item/field locations
39
+ const keyMappings = [];
40
+ const unknownKeys = [];
41
+ for (const key of keys) {
42
+ const ref = config.vault.secrets[key];
43
+ if (!ref) {
44
+ unknownKeys.push(key);
45
+ continue;
46
+ }
47
+ const { item, field } = parseOpRef(ref);
48
+ keyMappings.push({ key, item, field });
49
+ }
50
+ if (unknownKeys.length > 0) {
51
+ throw new Error(`No secret config for keys: ${unknownKeys.join(', ')}`);
52
+ }
53
+ // Group by item name to minimize 1Password calls
54
+ const itemNames = [...new Set(keyMappings.map((m) => m.item))];
55
+ // Fetch all items in parallel
56
+ const items = await op.getItemsBatch(vaultName, itemNames);
57
+ // Extract requested fields from fetched items
58
+ const result = {};
59
+ const missingSecrets = [];
60
+ for (const { key, item, field } of keyMappings) {
61
+ const opItem = items.get(item);
62
+ if (!opItem) {
63
+ missingSecrets.push(`op://${vaultName}/${item}/${field} (item not found)`);
64
+ continue;
65
+ }
66
+ const value = opItem.fields[field];
67
+ if (value === undefined) {
68
+ missingSecrets.push(`op://${vaultName}/${item}/${field} (field not found)`);
69
+ continue;
70
+ }
71
+ result[key] = value;
72
+ }
73
+ if (missingSecrets.length > 0) {
74
+ throw new Error(`Failed to fetch secrets from 1Password:\n - ${missingSecrets.join('\n - ')}`);
75
+ }
76
+ return result;
77
+ },
78
+ write: async (values, context) => {
79
+ const ctx = z.object({ env: z.enum(envValues) }).parse(context);
80
+ const vaultName = config.vaults[ctx.env];
81
+ if (!vaultName) {
82
+ throw new Error(`No vault configured for environment: ${ctx.env}`);
83
+ }
84
+ // Group values by 1Password item for batch writes
85
+ const byItem = new Map();
86
+ for (const [key, value] of Object.entries(values)) {
87
+ if (value === undefined)
88
+ continue;
89
+ const ref = config.vault.secrets[key];
90
+ if (!ref) {
91
+ throw new Error(`Unknown variable "${key}". ` + `Ensure it is declared in the vault configuration.`);
92
+ }
93
+ const { item, field } = parseOpRef(ref);
94
+ if (!byItem.has(item)) {
95
+ byItem.set(item, {});
96
+ }
97
+ byItem.get(item)[field] = value;
98
+ }
99
+ // Batch write each item (fail fast on error)
100
+ for (const [item, fields] of byItem) {
101
+ await op.setFieldsBatch(vaultName, item, fields);
102
+ }
103
+ },
104
+ };
105
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Vault definition for 1Password secrets.
3
+ *
4
+ * Each entry maps a variable name to an "item:field" reference.
5
+ * Format: `"itemName:fieldName"` where both are required.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const vault = defineOpVault({
10
+ * POSTGRES_URL: 'supabase:SUPABASE_SESSION_DSN',
11
+ * SUPABASE_URL: 'supabase:SUPABASE_URL',
12
+ * STRIPE_SECRET_KEY: 'stripe:STRIPE_SECRET_KEY',
13
+ * });
14
+ * ```
15
+ */
16
+ export type OpVaultRef = `${string}:${string}`;
17
+ export type OpVault<TSecrets extends Record<string, OpVaultRef>> = {
18
+ secrets: TSecrets;
19
+ };
20
+ export type InferOpVaultKeys<T> = T extends OpVault<infer S> ? keyof S : never;
21
+ /**
22
+ * Parse an "item:field" reference into its components.
23
+ */
24
+ export declare function parseOpRef(ref: OpVaultRef): {
25
+ item: string;
26
+ field: string;
27
+ };
28
+ /**
29
+ * Define a vault with typed secret declarations.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * const vault = defineOpVault({
34
+ * POSTGRES_URL: 'supabase:SUPABASE_SESSION_DSN',
35
+ * SUPABASE_URL: 'supabase:SUPABASE_URL',
36
+ * VITE_SUPABASE_URL: 'supabase:SUPABASE_URL',
37
+ * });
38
+ * ```
39
+ */
40
+ export declare function defineOpVault<const TSecrets extends Record<string, OpVaultRef>>(secrets: TSecrets): OpVault<TSecrets>;
41
+ //# sourceMappingURL=vault.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vault.d.ts","sourceRoot":"","sources":["../src/vault.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,MAAM,MAAM,UAAU,GAAG,GAAG,MAAM,IAAI,MAAM,EAAE,CAAC;AAE/C,MAAM,MAAM,OAAO,CAAC,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,IAAI;IACjE,OAAO,EAAE,QAAQ,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,KAAK,CAAC;AAE/E;;GAEG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,UAAU,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAS3E;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAAC,KAAK,CAAC,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,EAC7E,OAAO,EAAE,QAAQ,GAChB,OAAO,CAAC,QAAQ,CAAC,CAEnB"}
package/dist/vault.js ADDED
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Vault definition for 1Password secrets.
3
+ *
4
+ * Each entry maps a variable name to an "item:field" reference.
5
+ * Format: `"itemName:fieldName"` where both are required.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const vault = defineOpVault({
10
+ * POSTGRES_URL: 'supabase:SUPABASE_SESSION_DSN',
11
+ * SUPABASE_URL: 'supabase:SUPABASE_URL',
12
+ * STRIPE_SECRET_KEY: 'stripe:STRIPE_SECRET_KEY',
13
+ * });
14
+ * ```
15
+ */
16
+ /**
17
+ * Parse an "item:field" reference into its components.
18
+ */
19
+ export function parseOpRef(ref) {
20
+ const colonIndex = ref.indexOf(':');
21
+ if (colonIndex === -1) {
22
+ throw new Error(`Invalid vault reference: ${ref}. Expected "item:field".`);
23
+ }
24
+ return {
25
+ item: ref.slice(0, colonIndex),
26
+ field: ref.slice(colonIndex + 1),
27
+ };
28
+ }
29
+ /**
30
+ * Define a vault with typed secret declarations.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * const vault = defineOpVault({
35
+ * POSTGRES_URL: 'supabase:SUPABASE_SESSION_DSN',
36
+ * SUPABASE_URL: 'supabase:SUPABASE_URL',
37
+ * VITE_SUPABASE_URL: 'supabase:SUPABASE_URL',
38
+ * });
39
+ * ```
40
+ */
41
+ export function defineOpVault(secrets) {
42
+ return { secrets };
43
+ }
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@pokit/op",
3
+ "version": "0.0.1",
4
+ "description": "Operation utilities for pok CLI applications",
5
+ "keywords": [
6
+ "cli",
7
+ "command-line",
8
+ "typescript",
9
+ "bun",
10
+ "terminal",
11
+ "pok",
12
+ "pokjs",
13
+ "operations",
14
+ "utilities",
15
+ "helpers"
16
+ ],
17
+ "type": "module",
18
+ "license": "MIT",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/notation-dev/openpok.git",
22
+ "directory": "packages/op"
23
+ },
24
+ "homepage": "https://github.com/notation-dev/openpok#readme",
25
+ "bugs": {
26
+ "url": "https://github.com/notation-dev/openpok/issues"
27
+ },
28
+ "main": "./src/index.ts",
29
+ "module": "./src/index.ts",
30
+ "types": "./src/index.ts",
31
+ "exports": {
32
+ ".": {
33
+ "bun": "./src/index.ts",
34
+ "types": "./dist/index.d.ts",
35
+ "import": "./dist/index.js"
36
+ }
37
+ },
38
+ "files": [
39
+ "dist",
40
+ "README.md",
41
+ "LICENSE"
42
+ ],
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "dependencies": {
47
+ "@pokit/core": "0.0.1"
48
+ },
49
+ "devDependencies": {
50
+ "@types/bun": "latest"
51
+ },
52
+ "peerDependencies": {
53
+ "zod": "^4.0.0"
54
+ },
55
+ "engines": {
56
+ "bun": ">=1.0.0"
57
+ }
58
+ }
package/src/index.ts ADDED
@@ -0,0 +1,28 @@
1
+ export { defineOpVault, parseOpRef } from './vault';
2
+ export type { OpVault, OpVaultRef, InferOpVaultKeys } from './vault';
3
+
4
+ export { defineOpResolver } from './resolver';
5
+ export type { OpResolverConfig } from './resolver';
6
+
7
+ // =============================================================================
8
+ // 1Password CLI utilities
9
+ // =============================================================================
10
+
11
+ export * as opUtils from './op';
12
+
13
+ export type { OpItem } from './op';
14
+
15
+ /**
16
+ * Type exports for individual utility functions.
17
+ * These enable consumers to reference specific function signatures
18
+ * without accessing them through the opUtils namespace.
19
+ */
20
+ export type IsInstalledFn = typeof import('./op').isInstalled;
21
+ export type IsAuthenticatedFn = typeof import('./op').isAuthenticated;
22
+ export type GetFieldFn = typeof import('./op').getField;
23
+
24
+ // =============================================================================
25
+ // 1Password CLI checks
26
+ // =============================================================================
27
+
28
+ export { opInstalled, opAuthenticated } from './checks';