@1password/op-js

中文文档
0.1.13发布于 2024/06/21MITTypeScriptCommonJS近 30 天下载 26.2万

A typed JS wrapper for the 1Password CLI

原始使用文档
文档目录8 节
  1. Installation
  2. Usage
  3. Flags
  4. Validating the CLI
  5. Authentication
  6. Available commands and functions
  7. Contributing and feedback
  8. License

op-js

This package is a simple JavaScript wrapper for the 1Password CLI. It provides methods for most of the CLI's commands, and in many cases extends the CLI's ability to authenticate using biometrics to whatever Node-based application you're building. It also includes TypeScript declarations.

Installation

Install using Yarn:

yarn add @1password/op-js

Or using NPM:

npm install @1password/op-js

Usage

After installation you can start using command methods:

import { version, item, connect } from "@1password/op-js";

// Some command functions may be directly imported
version();

// But most exist on their parent command's object
item.get("x1oszeq62e2ys32v9a3l2sgcwly");

// And sub-commands are nested even further
connect.group.revoke({
    group: "MyGroup",
    allServers: true,
});

The CLI takes flags as kebab-case, however to align better with JS object convention all flags should be provided as camelCase.

Flags

All command methods support support global command flags, as well as their own flags, but this package also provides a helper to set global command flags do you don't need to each time. For example:

import { setGlobalFlags } from "@1password/op-js";

setGlobalFlags({
    account: "example.1password.com",
});

Note that you should not try to set the --format flag as this is set under the hood to json for all commands that can return JSON format; it is otherwise a string response.

Validating the CLI

Since this package depends on the 1Password CLI it's up to the user to install it, and the types may depend on a specific version. There is a function that your application can call to validate that the user has the CLI installed at a specific version:

import { validateCli } from "@1password/op-js";

validateCli().catch((error) => {
    console.log("CLI is not valid:", error.message);
});

// defaults to the recommended version, but you can supply a semver:
validateCli(">=2.3.1").catch((error) => {
    console.log("CLI is not valid:", error.message);
});

Authentication

By default op-js uses system authentication (e.g. biometrics), but it also supports automated authentication via Connect Server or Service Account.

Connect

If you've got a Connect Server set up you can supply your host and token:

import { setConnect } from "@1password/op-js";

setConnect("https://connect.myserver.com", "1kjhd9872hd981865s");

Alternatively you can use environment variables when executing the code that uses op-js:

OP_CONNECT_HOST=https://connect.myserver.com
OP_CONNECT_TOKEN=1kjhd9872hd981865s

Service Account

If you're using service accounts you can supply your token:

import { setServiceAccount } from "@1password/op-js";

setServiceAccount("1kjhd9872hd981865s");

Alternatively you can use environment variables when executing the code that uses op-js:

OP_SERVICE_ACCOUNT_TOKEN=1kjhd9872hd981865s

Available commands and functions

There are roughly 70 commands available for use, so you're encouraged to check out the main index.ts file to get a better sense of what's available. Generally, though, here are the top-level commands/namespaces you can import:

  • version - Retrieve the current version of the CLI
  • inject - Inject secrets into a config file
  • read - Read a secret by secret references
  • account - Manage accounts
  • document - Manage documents in a vault
  • eventsApi - Create an Events API integration token
  • connect - Manage Connect groups, services, tokens, and vaults
  • item - Manage vault items and templates
  • vault - Manage account vaults
  • user - Manage account users
  • group - Manage groups and their users
  • whoami - Get details about the authenticated account

Contributing and feedback

🐛 If you find an issue you'd like to report, or otherwise have feedback, please file a new Issue.

🧑‍💻 If you'd like to contribute to the project please start by filing or commenting on an Issue so we can track the work. Refer to the Contributing doc for development setup instructions.

💬 Share your feedback and connect with the Developer Products team in the 1Password Developers Slack workspace.

License

MIT

版本与发布记录

@1password/op-js 共发布 19 个版本,首次发布于 2022/06/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.14-alpha.52025/08/011.3 年前
0.1.14-alpha.42025/08/011.3 年前
0.1.14-alpha.32025/08/011.3 年前
0.1.14-alpha.22025/08/011.3 年前
0.1.14-alpha.12025/08/011.3 年前
0.1.13latest2024/06/212 年前

关于 @1password/op-js 的常见问题

@1password/op-js 是什么?

A typed JS wrapper for the 1Password CLI

如何安装 @1password/op-js?

在项目目录运行 npm install @1password/op-js,或使用 pnpm add / yarn add / bun add。

@1password/op-js 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@1password/op-js 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@1password/op-js 可以商用吗?

@1password/op-js 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@1password/op-js 的下载量有多少?

根据 npm 官方下载统计,@1password/op-js 在 2026/10/05 – 2026/11/03 这 30 天内被下载 262,208 次,日均约 8,740 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@1password/op-js 还在维护吗?

最新版本 0.1.13 发布于 2024/06/21(2 年前);至今共发布 19 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/06/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。