@data-fair/lib-common-types

中文文档
1.27.0发布于 2026/10/19MITES Module近 30 天下载 2139

Shared schemas and built type definitions in the data-fair stack.

原始使用文档
文档目录7 节
  1. Install
  2. Modules
  3. Usage
  4. Session
  5. Referencing the shared schemas
  6. Plugins
  7. Scopes

@data-fair/lib-common-types

The JSON schemas and TypeScript types shared across the data-fair stack (session state, accounts, events, notifications, themes, catalog and processing plugin contracts…), with their generated validators and a few helpers built on top of them. Services, Vue applications and plugins import it so that they all speak the same contracts.

Install

npm i @data-fair/lib-common-types

Optional peer dependency: tinycolor2 (v1), only needed by theme/index.js.

Modules

Each directory module exports its generated types (and validate / assertValid / returnValid when listed) from index.js, and its raw JSON schema as the default export of schema.js.

Import pathDescription
@data-fair/lib-common-types/session/index.jsSessionState, SessionStateAuthenticated, User, Account… types, validators, and the isAuthenticated, assertAuthenticated, assertAdminMode, getAccountRole, assertAccountRole, isValidAccountType, assertValidAccountType helpers.
@data-fair/lib-common-types/account/index.jsAccount (owner of a resource) and AccountKeys types, validators.
@data-fair/lib-common-types/access-ref/index.jsAccessRef permission target types, validators, matchAccessRef(session, accessRef) and mongoFilterAccessRef(session).
@data-fair/lib-common-types/application/index.jsApplication, Dataset and Field types, as seen by a data-fair application.
@data-fair/lib-common-types/event/index.jsEvent types (events service).
@data-fair/lib-common-types/notification/index.jsNotification types, validators.
@data-fair/lib-common-types/theme/index.jsTheme / Colors types, defaultTheme, fillTheme, color contrast helpers (getReadableColor, getOnColor, getColorsWarnings, getSiteColorsWarnings…).
@data-fair/lib-common-types/catalog/index.jsThe CatalogPlugin contract of catalog plugins and its context types.
@data-fair/lib-common-types/processings.jsThe PrepareFunction, RunFunction, ProcessingContext and LogFunctions types of processing plugins.
@data-fair/lib-common-types/ws.jsMessage type of the websocket protocol.
@data-fair/lib-common-types/scopes.jsdefineScopes (a service's scope vocabulary: accepted scopes per tier, descriptions, global tier scopes), globalScopes, oauth2SecurityScheme.

Usage

Session

The session state is usually obtained through the session middleware of @data-fair/lib-express or useSession of @data-fair/lib-vue; this package provides its type and the checks. See the data-fair-session skill.

import { type SessionState, assertAccountRole } from '@data-fair/lib-common-types/session/index.js'

// throws httpError(401) if not authenticated, httpError(403) if the active account does not have the role
assertAccountRole(sessionState, resource.owner, 'admin')

getAccountRole(sessionState, account, { allAccounts?, acceptDepAsRoot? }) returns the role instead of throwing ('admin' in admin mode and for the user's own account, null if none).

Referencing the shared schemas

Schemas built with @data-fair/lib-types-builder can $ref the shared schemas by their $id (https://github.com/data-fair/lib/<name>):

owner: { $ref: 'https://github.com/data-fair/lib/account' },
topic: { $ref: 'https://github.com/data-fair/lib/event#/$defs/topicRef' }

Plugins

import type { PrepareFunction, RunFunction, ProcessingContext } from '@data-fair/lib-common-types/processings.js'
import type { CatalogPlugin } from '@data-fair/lib-common-types/catalog/index.js'

See the data-fair-processing skill.

Scopes

A service declares its scope families once; the matching of session/index.js (hasScope, exact or bare umbrella, never prefix) stays the same.

import { defineScopes, oauth2SecurityScheme, oauth2Alternatives } from '@data-fair/lib-common-types/scopes.js'

const scopes = defineScopes({
  notifications: {
    tiers: ['read', 'write'],
    descriptions: {
      umbrella: { fr: 'Tous les droits sur les notifications', en: 'All notification scopes' },
      read: { fr: 'Lire ses notifications', en: 'Read own notifications' },
      write: { fr: 'Gérer ses abonnements', en: 'Manage own subscriptions' }
    }
  }
})

// notifications:write notifications all:write all:admin
requireScope(scopes.acceptedScopes('notifications', 'write'))

// the oauth2 scheme of the service's OpenAPI document
oauth2SecurityScheme(scopes, { directoryUrl, locale: 'fr' })

// an operation's security: [{ sdCookie: [] }, { oauth2: ['notifications:write'] }, { oauth2: ['notifications'] }, ...]
const security = [{ sdCookie: [] }, ...oauth2Alternatives(scopes.acceptedScopes('notifications', 'write'))]
  • acceptedScopes(family, tier): the family's declared tiers at or above tier, the umbrella, then all:<tier> and the higher global tier scopes. A tier the family does not declare throws.
  • familyScopes(family): every scope that reaches some tier of the family, for a coarse gate.
  • scopeDescriptions(locale): the description of every declared scope, the all:* ones included.
  • oauth2Alternatives(accepted): one OpenAPI security alternative per accepted scope. A single { oauth2: [a, b] } would mean a token needs both. What an all:* scope grants is read from the operations that list it, data-fair's platform document relies on this form.
  • The global tier scopes and their descriptions are owned here (globalScopes), identical in every service. all is reserved as a family name.

版本与发布记录

@data-fair/lib-common-types 共发布 73 个版本,首次发布于 2024/10/07;过去 12 个月发布了 22 个版本。

版本发布日期距今
1.27.0latest2026/10/1924 天前
1.26.02026/10/1925 天前
1.25.02026/10/1628 天前
1.24.02026/09/231 个月前
1.23.12026/09/082 个月前
1.23.02026/09/012 个月前

关于 @data-fair/lib-common-types 的常见问题

@data-fair/lib-common-types 是什么?

Shared schemas and built type definitions in the data-fair stack.

如何安装 @data-fair/lib-common-types?

在项目目录运行 npm install @data-fair/lib-common-types,或使用 pnpm add / yarn add / bun add。

@data-fair/lib-common-types 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/data-fair__lib-common-types 是否存在。

@data-fair/lib-common-types 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@data-fair/lib-common-types 可以商用吗?

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

@data-fair/lib-common-types 的下载量有多少?

根据 npm 官方下载统计,@data-fair/lib-common-types 在 2026/10/12 – 2026/11/10 这 30 天内被下载 2,139 次,日均约 71 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@data-fair/lib-common-types 还在维护吗?

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