|
| 1 | +import { OryRelationshipsService } from '@getlarge/keto-client-wrapper'; |
| 2 | +import { |
| 3 | + createRelationQuery, |
| 4 | + parseRelationTuple, |
| 5 | +} from '@getlarge/keto-relations-parser'; |
| 6 | +import { Logger } from '@nestjs/common'; |
| 7 | +import { Configuration, type RelationQuery } from '@ory/client'; |
| 8 | +import { Command, CommandRunner, Option } from 'nest-commander'; |
| 9 | + |
| 10 | +interface CommandOptions |
| 11 | + extends Pick<Configuration, 'basePath' | 'accessToken'> { |
| 12 | + tuple: RelationQuery; |
| 13 | +} |
| 14 | + |
| 15 | +@Command({ name: 'create', description: 'Create relationship on Ory Keto' }) |
| 16 | +export class CreateRelationCommand extends CommandRunner { |
| 17 | + readonly logger = new Logger(CreateRelationCommand.name); |
| 18 | + |
| 19 | + constructor( |
| 20 | + private readonly oryRelationshipsService: OryRelationshipsService |
| 21 | + ) { |
| 22 | + super(); |
| 23 | + } |
| 24 | + |
| 25 | + async run(passedParams: string[], options: CommandOptions): Promise<void> { |
| 26 | + console.log('options', options); |
| 27 | + const { tuple } = options; |
| 28 | + if (options.accessToken || options.basePath) { |
| 29 | + this.oryRelationshipsService.config = new Configuration({ |
| 30 | + ...this.oryRelationshipsService.config, |
| 31 | + ...options, |
| 32 | + }); |
| 33 | + } |
| 34 | + await this.oryRelationshipsService.createRelationship({ |
| 35 | + createRelationshipBody: tuple, |
| 36 | + }); |
| 37 | + this.logger.debug('Created relation'); |
| 38 | + this.logger.log(tuple); |
| 39 | + } |
| 40 | + |
| 41 | + @Option({ |
| 42 | + flags: '-t, --tuple [string]', |
| 43 | + description: 'Relationship tuple to create, using Zanzibar notation', |
| 44 | + required: true, |
| 45 | + }) |
| 46 | + parseRelationTuple(val: string): RelationQuery { |
| 47 | + const res = parseRelationTuple(val); |
| 48 | + if (res.hasError()) { |
| 49 | + throw res.error; |
| 50 | + } |
| 51 | + return createRelationQuery(res.value).unwrapOrThrow(); |
| 52 | + } |
| 53 | + |
| 54 | + @Option({ |
| 55 | + flags: '-b, --basePath [string]', |
| 56 | + description: 'Ory Keto Admin URL', |
| 57 | + required: false, |
| 58 | + }) |
| 59 | + parseBasePath(val: string): string | undefined { |
| 60 | + return val; |
| 61 | + } |
| 62 | + |
| 63 | + @Option({ |
| 64 | + flags: '-a, --accessToken [string]', |
| 65 | + description: 'Ory Keto Access Token', |
| 66 | + required: false, |
| 67 | + }) |
| 68 | + parseAccessToken(val: string): string | undefined { |
| 69 | + return val ?? process.env['ORY_KETO_API_KEY']; |
| 70 | + } |
| 71 | +} |
0 commit comments