Skip to content

Commit d623a60

Browse files
committed
use new sdk instead of cli
1 parent 30273db commit d623a60

File tree

6 files changed

+54
-49
lines changed

6 files changed

+54
-49
lines changed

examples/plugin-test/.env.op

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# # @plugin(./node_modules/@varlock/1password-plugin)
77
#
88
# initialize the plugin, wiring up settings and auth
9-
# @initOp(id=v1, account=dmnoinc, token=$OP_TOKEN, allowAppAuth=forEnv(dev))
9+
# @initOp(id=v1, account="DMNO Inc.", token=$OP_TOKEN, allowAppAuth=forEnv(dev))
1010
# @initOp(id=v2, account=dmnoinc, token=$OP_TOKEN)
1111
#
1212
# @defaultRequired=infer @defaultSensitive=false

packages/plugins/1password/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
"varlock": "workspace:^"
4242
},
4343
"devDependencies": {
44-
"@1password/sdk": "^0.3.1",
45-
"@1password/sdk-core": "^0.3.1",
44+
"@1password/sdk": "0.4.0-beta.1",
45+
"@1password/sdk-core": "0.4.0-beta.1",
4646
"@env-spec/utils": "workspace:^",
4747
"@types/node": "catalog:",
4848
"import-meta-resolve": "^4.2.0",

packages/plugins/1password/src/plugin.ts

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Resolver } from 'varlock/plugin-lib';
22

33
import { createDeferredPromise, DeferredPromise } from '@env-spec/utils/defer';
4-
import { Client, createClient } from '@1password/sdk';
4+
import { Client, createClient, DesktopAuth } from '@1password/sdk';
55
import { opCliRead } from './cli-helper';
66

77
const { ValidationError, SchemaError, ResolutionError } = plugin.ERRORS;
@@ -39,43 +39,47 @@ class OpPluginInstance {
3939

4040
opClientPromise: Promise<Client> | undefined;
4141
async initSdkClient() {
42-
if (!this.token) return;
43-
if (this.opClientPromise) return;
42+
if (this.opClientPromise) return this.opClientPromise;
4443

45-
// TODO: pass through account once SDK allows it
46-
this.opClientPromise = createClient({
47-
auth: this.token,
48-
integrationName: 'varlock plugin',
49-
integrationVersion: PLUGIN_VERSION,
50-
});
44+
if (this.token) {
45+
// TODO: pass through account once SDK allows it
46+
this.opClientPromise = createClient({
47+
auth: this.token,
48+
integrationName: 'varlock plugin',
49+
integrationVersion: PLUGIN_VERSION,
50+
});
51+
} else {
52+
if (!this.account) throw new Error('account must be set to use desktop auth');
53+
this.opClientPromise = createClient({
54+
auth: new DesktopAuth(this.account),
55+
integrationName: 'varlock plugin',
56+
integrationVersion: PLUGIN_VERSION,
57+
});
58+
}
59+
return this.opClientPromise;
5160
}
5261

5362
readBatch?: Record<string, { defers: Array<DeferredPromise<string>> }> | undefined;
5463

5564
async readItem(opReference: string) {
56-
if (this.token) {
65+
if (this.token || this.allowAppAuth) {
5766
// using JS SDK client using service account token
5867
await this.initSdkClient();
59-
if (this.opClientPromise) {
60-
// simple batching setup, so we can use bulk read sdk method
61-
let triggerBatch = false;
62-
if (!this.readBatch) {
63-
this.readBatch = {};
64-
triggerBatch = true;
65-
}
66-
// add item to batch, with deferred promise
67-
this.readBatch[opReference] = { defers: [] };
68-
const deferred = createDeferredPromise();
69-
this.readBatch[opReference].defers.push(deferred);
70-
if (triggerBatch) {
71-
setImmediate(() => this.executeReadBatch());
72-
}
73-
return deferred.promise;
68+
69+
// simple batching setup, so we can use bulk read sdk method
70+
let triggerBatch = false;
71+
if (!this.readBatch) {
72+
this.readBatch = {};
73+
triggerBatch = true;
74+
}
75+
// add item to batch, with deferred promise
76+
this.readBatch[opReference] = { defers: [] };
77+
const deferred = createDeferredPromise<string>();
78+
this.readBatch[opReference].defers.push(deferred);
79+
if (triggerBatch) {
80+
setImmediate(() => this.executeReadBatch());
7481
}
75-
} else if (this.allowAppAuth) {
76-
// using op CLI to talk to 1Password desktop app
77-
// NOTE - cli helper does its own batching, untethered to a specific op instance
78-
return await opCliRead(opReference, this.account);
82+
return deferred.promise;
7983
} else {
8084
throw new SchemaError('Unable to authenticate with 1Password', {
8185
tip: `Plugin instance (${this.id}) must be provided either a service account token or have app auth enabled (allowAppAuth=true)`,

packages/plugins/1password/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"compilerOptions": {
3-
"target": "es2021",
3+
"lib": ["es2022"],
4+
"target": "es2022",
45
"moduleResolution": "bundler",
56
"strict": true,
67
"resolveJsonModule": true,

packages/utils/src/defer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function createDeferredPromise<T = unknown>() {
66
// set to noop, but they will be replaced immediately
77
let resolve: (value?: T) => void = () => {};
88
let reject: (reason?: unknown) => void = () => {};
9-
const promise = new Promise((_resolve, _reject) => {
9+
const promise = new Promise<T | undefined>((_resolve, _reject) => {
1010
resolve = _resolve;
1111
reject = _reject;
1212
});

pnpm-lock.yaml

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)