keto- 客户端包装器
这个图书馆是一个包装 周围的 奥里·克托 客户端 - @ ory/ 客户它规定:
OryRelationshipsModule: 与 Ory Keto 关系 API 互动的模块OryPermissionsModule: 与 Ory Keto 权限 API 交互的模块OryAuthorizationGuard: 一个根据Ory Keto权限保护您路线的警卫
安装
npm install @getlarge/keto-client-wrapper
使用量
导入应用程序中的模块 :
import {
OryRelationshipsModule,
OryPermissionsModule,
} from '@getlarge/keto-client-wrapper';
import { Module } from '@nestjs/common';
@Module({
imports: [
OryRelationshipsModule.forRoot({
basePath: 'http://localhost:4467',
accessToken: 'my-access-token',
}),
OryPermissionsModule.forRootAsync({
useFactory: () => ({
baseUrl: 'http://localhost:4466',
}),
}),
],
})
在您的提供者中注入服务 :
import { OryRelationshipsService } from '@getlarge/keto-client-wrapper';
import { Injectable } from '@nestjs/common';
@Injectable()
export class YourService {
constructor(
private readonly oryRelationshipsService: OryRelationshipsService
) {}
}
利用警卫保护你的路线:
import {
OryAuthorizationGuard,
OryPermissionChecks,
} from '@getlarge/keto-client-wrapper';
import { RelationTupleBuilder } from '@getlarge/keto-relations-parser';
import { Controller, Get, Logger, UseGuards } from '@nestjs/common';
@Controller()
export class YourController {
@OryPermissionChecks((ctx) => {
const req = ctx.switchToHttp().getRequest();
const currentUserId = req.headers['x-current-user-id'] as string;
const resourceId = req.params.id;
return new RelationTupleBuilder()
.subject('User', currentUserId)
.isIn('owners')
.of('Toy', resourceId)
.toString();
})
@UseGuards(
OryAuthorizationGuard({
postCheck(relationTuple, isPermitted) {
Logger.log('relationTuple', relationTuple);
Logger.log('isPermitted', isPermitted);
},
})
)
@Get()
getArticles() {
return 'Articles';
}
}
发展
大楼
运行 nx build keto-client-wrapper 来建造图书馆.
运行单元测试
运行 nx test keto-client-wrapper 通过 欢乐。 。 。 .