メインコンテンツまでスキップ
バージョン: 0.14.0 (最新)

🏷️ アトリビューション

アトリビューションは、attributesService CRUD APIを通じて名前付き文字列キーバリューアイテムのグループを管理します。

まずはここから

attributesServiceattributesidentities コレクションを必要とします。リストおよび読み取りルートは公開されており、作成・更新・削除には構成された管理者IDと一致するタイプを持つ認証済みアイデンティティが必要です。

attributesService(dataStores, configuration)createAttributeFeaturegetAttributeFeaturefindAttributesFeatureupdateAttributeFeaturedeleteAttributeFeature をこの順序で合成します。選択された認証アダプタ、構成、データストアをルーターパイプラインのたびに注入します。アトリビューションサービスauthユーティリティcookieユーティリティ を参照してください。

import express from 'express';
import cookieParser from 'cookie-parser';
import {services} from '@nodeblocks/backend-sdk';

const app = express();
app.use(express.json());
app.use(cookieParser()); // authMode: 'cookie' の場合にのみ必要。
app.use('/api', services.attributesService(
{attributes, identities},
{
authSecrets: {
authEncSecret: process.env.AUTH_ENC_SECRET!,
authSignSecret: process.env.AUTH_SIGN_SECRET!,
},
authMode: 'bearer',
identity: {typeIds: {admin: 'admin', guest: 'guest', regular: 'regular'}},
},
));
構成デフォルト/ソースの動作効果
authSecrets.authEncSecretauthSecrets.authSignSecret保護されたルートに必須;このサービスにはデフォルトなしアクセストークンを検証します。
authMode省略または 'bearer'getBearerTokenInfo を選択保護されたルートは Authorization ヘッダーからアクセストークンを読み取ります。
authMode: 'cookie'設定時に getCookieTokenInfo を選択保護されたルートはアクセストークンのクッキーを読み取ります;先に cookie-parser を登録してください。
identity.typeIds.admin保護されたルートに必須;checkIdentityType(['admin']) によって読み取られる管理者アイデンティティを識別します。
エクスポート契約
AttributesServiceDataStoreMongoDB の attributes および identities コレクションを必要とします。
AttributesServiceConfigurationauthSecrets を必須とし、authMode を選択してアイデンティティタイプIDを提供します。
AttributesServiceService<AttributesServiceDataStore, AttributesServiceConfiguration>
attributesService5つのアトリビューションフィーチャーコンポーザーからマウントされたExpressルーターを作成します。
サービス合成ソースを表示
export interface AttributesServiceDataStore {
attributes: Collection;
identities: Collection;
}

export interface AttributesServiceConfiguration {
authSecrets: {
authEncSecret: string;
authSignSecret: string;
};
authMode?: 'bearer' | 'cookie';
identity?: {
typeIds?: {
admin: string;
guest: string;
regular: string;
};
};
}

export type AttributesService = Service<
AttributesServiceDataStore,
AttributesServiceConfiguration
>;

export const attributesService: AttributesService = (dataStores, configuration) => {
return defService(
partial(
compose(
createAttributeFeature,
getAttributeFeature,
findAttributesFeature,
updateAttributeFeature,
deleteAttributeFeature
),
[{
authenticate: configuration.authMode === 'cookie'
? getCookieTokenInfo
: getBearerTokenInfo,
configuration,
dataStores,
}]
)
);
};

よくあるタスク

タスク最初のステップ契約
アトリビューショングループのリストfindAttributesFeatureリストルート および クエリスキーマ
1つのグループの読み取りgetAttributeFeature取得ルート および パススキーマ
グループの作成createAttributeFeature作成ルート および 作成スキーマ
グルームのリネームupdateAttributeFeature更新ルートname のみが受理されます
グループの削除deleteAttributeFeature削除ルート および パススキーマ
選択APIの構築フィーチャーコンポーザー複合サービスガイド

Bearer HTTP ワークフロー

API_BASE_URL='http://localhost:8080/api'
ATTRIBUTE_ID='replace-with-an-existing-attribute-id'
ADMIN_ACCESS_TOKEN='replace-with-an-administrator-access-token'

curl "$API_BASE_URL/attributes?name=Product&page=1&limit=20"

curl "$API_BASE_URL/attributes/$ATTRIBUTE_ID"

curl -X POST "$API_BASE_URL/attributes" \
-H "authorization: Bearer $ADMIN_ACCESS_TOKEN" \
-H 'content-type: application/json' \
-d '{"name":"Product options","items":[{"key":"color","value":"red"}]}'

リストおよび読み取りルートは 200 を返します。作成は作成されたグループを 201 で返します;無効/不足な管理者資格証明書はパイプラインの前に失敗します。リンクされた作成スキーマ作成ルート、およびバリデーター が完全な契約を定義します。

authMode: 'cookie' を設定し、サービスの前に cookie-parser を登録し、Authentication ログインルートからアクセスクッキージールを取得します。以下の資格証明書は管理者アイデンティティに属している必要があります。

API_BASE_URL='http://localhost:8080/api'
ATTRIBUTE_ID='replace-with-an-existing-attribute-id'
ADMIN_EMAIL='admin@example.com'
ADMIN_PASSWORD='replace-with-the-admin-password'

curl -c cookies.txt -X POST "$API_BASE_URL/auth/login" \
-H 'content-type: application/json' \
-d "{\"email\":\"$ADMIN_EMAIL\",\"password\":\"$ADMIN_PASSWORD\"}"

curl -b cookies.txt -X PATCH "$API_BASE_URL/attributes/$ATTRIBUTE_ID" \
-H 'content-type: application/json' \
-d '{"name":"Updated product options"}'

リクエストには依然として管理者アイデンティティが必要です。更新スキーマは空のオブジェクトを受け入れますが、ハンドラーは空のボディを 400 で拒否します。セッションの更新およびログアウトについては、Authentication クッキーワークフロー を参照してください。

カスタムフィーチャー合成

この完全なフラグメントは、作成およびリストルートのみを含むルーターを構築します。あらかじめ用意された attributesidentitiesauthSecrets、および typeIds 変数を前提としています。

import {partial} from 'ramda';
import {features, primitives, utils} from '@nodeblocks/backend-sdk';

const attributeFeatureComposer = primitives.compose(
features.createAttributeFeature,
features.findAttributesFeature,
);

const attributeRouter = primitives.defService(partial(attributeFeatureComposer, [{
authenticate: utils.getBearerTokenInfo,
configuration: {authSecrets, identity: {typeIds}},
dataStores: {attributes, identities},
}]));

app.use('/api', attributeRouter);

リファレンスマップ

ページ目的
フィーチャースキーマからルートへのコンポーザー。
ハンドラーパイプライン操作とターミネーター。
ルートHTTP契約および完全なルートソース。
スキーマフィールドレベルの要求契約。
バリデーターミューテーションによって共有される管理者アクセスチェック。

アトリビューションには blocks.md ページがありません。SDK はアトリビューション固有のブロックレイヤーをエクスポートしません。

関連モジュール

広範なサービス参照にはアトリビューションサービス、セッションの発行には Authentication、共有スキーマ動作には スキーマコンポーネント、アプリケーションレベルのエラーには エラーハンドリング を使用してください。