{"id":23773885,"url":"https://github.com/StyraOSS/opa-typescript-example-nestjs","last_synced_at":"2026-04-03T19:30:17.621Z","repository":{"id":228027338,"uuid":"771545941","full_name":"StyraInc/opa-typescript-example-nestjs","owner":"StyraInc","description":"example project using opa-typescript with a simple nestjs REST service","archived":false,"fork":false,"pushed_at":"2024-04-19T10:26:36.000Z","size":1693,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-04-19T21:15:14.785Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/StyraInc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-03-13T13:53:48.000Z","updated_at":"2024-04-22T10:50:55.223Z","dependencies_parsed_at":"2024-04-22T10:50:50.452Z","dependency_job_id":null,"html_url":"https://github.com/StyraInc/opa-typescript-example-nestjs","commit_stats":null,"previous_names":["styrainc/opa-typescript-example-nestjs"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StyraInc%2Fopa-typescript-example-nestjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StyraInc%2Fopa-typescript-example-nestjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StyraInc%2Fopa-typescript-example-nestjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StyraInc%2Fopa-typescript-example-nestjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/StyraInc","download_url":"https://codeload.github.com/StyraInc/opa-typescript-example-nestjs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239964497,"owners_count":19725950,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-01T05:46:10.489Z","updated_at":"2026-04-03T19:30:17.567Z","avatar_url":"https://github.com/StyraInc.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# opa-typescript NestJS example\n\n\u003e [!IMPORTANT]\n\u003e **This code has been moved to [StyraInc/opa-sdk-demos](https://github.com/StyraInc/opa-sdk-demos).**\n\n## Description\n\nBased on the [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.\n\n## Input Payloads from Authz Decorators\n\n### Static key/val pairs\n\nUse the `Authz` decorator to add static information to the request payload sent to OPA:\n\n```ts\n@Authz(() =\u003e ({ resource: 'cat' }))\n```\n\non either the _class_ or the _handler_ method will add\n\n```json\n{ \"resource\": \"cat\" }\n```\n\nto each request payload.\n\nThere's a `AuthzStatic()` variant that lets you condense this to\n\n```ts\n@Authz({ resource: 'cat' })\n```\n\nFeel free to\n\n```ts\nimport { AuthzStatic as Authz } from '../authz/decorators/action';\n```\n\nif the dynamic functionality isn't required.\n\n### Forward request body\n\n```http\nPOST /cats\n{\"name\": \"garfield\", \"age\": 5, \"breed\": \"unknown\"}\n```\n\nThe entire request body can be forwarded:\n\n```ts\n@Authz(({ body }) =\u003e body)\n```\n\n```json\n{\n  \"name\": \"garfield\",\n  \"age\": 5,\n  \"breed\": \"unknown\"\n}\n```\n\nNote that we can also add a static part to this:\n\n```ts\n@Authz(({ body }) =\u003e ({ ...body, action: 'create' }))\n```\n\n```json\n{\n  \"name\": \"garfield\",\n  \"age\": 5,\n  \"breed\": \"unknown\",\n  \"action\": \"create\"\n}\n```\n\nIt's also possible to select just parts of the body:\n\n```ts\n@Authz(({ body: { name } }) =\u003e ({ name, action: 'create' }))\n```\n\n```json\n{\n  \"name\": \"garfield\",\n  \"action\": \"create\"\n}\n```\n\nIf you want to put the body into some key on the payload, use\n\n```ts\n@Authz(({ body }) =\u003e ({ payload: body }))\n```\n\n```json\n{\n  \"payload\": {\n    \"name\": \"garfield\",\n    \"age\": 5,\n    \"breed\": \"unknown\"\n  }\n}\n```\n\nRoute parameters can also be injected:\n\n```ts\n@Get(':name')\n@Authz(({ params }) =\u003e ({ ...params, action: 'get' }))\nasync findByName(@Param('name') name: string): Promise\u003cCat\u003e {\n  return this.catsService.findByName(name);\n}\n```\n\n```json\n{\n  \"name\": \"garfield\",\n  \"action\": \"get\"\n}\n```\n\n### More Request Details\n\nUnder the hood, the `@Authz` decorator registers functions that (optionally) take a `Request` parameter.\nSee [the NestJS docs for all the fields that you can use](https://docs.nestjs.com/controllers#request-object).\n\nPass along the client IP via\n\n```ts\n@Authz(({ ip }) =\u003e ({ ip }))\n```\n\nOr one specific header,\n\n```ts\n@Authz(({ headers: { 'x-user': u } }) =\u003e ({\n  ['x-user']: u,\n}))\n```\n\n## Response payload mapping\n\nUse the `@Decision` and `@DecisionStatic` decorators to instruct the AuthzGuard on how to get a boolean decision from the OPA response:\n\n```ts\n@DecisionStatic('allowed')\n```\n\nwill use the `allow` field of an object response. So if `POST /v1/data/cats/result` returns\n\n```json\n{\n  \"allowed\": true,\n  \"details\": []\n}\n```\n\nthe AuthzGuard will use the `allowed` field to make the decision.\n\nThe same be achieved using a non-static `@Decision` decorator:\n\n```ts\n@Decision((r) =\u003e r['allowed'])\n```\n\nNote that you can also pass a function, like\n\n```ts\nfunction pluckAllow(r: any): boolean {\n  return r['allow'];\n}\n```\n\nvia\n\n```ts\n@Decision(pluckAllow)\n```\n\nYou can use all of the result for the decision, for example\n\n```ts\n@Decision((r) =\u003e r.violations.length == 0)\n```\n\n## Installation\n\n```bash\n$ npm install\n```\n\n## Running the app\n\n```bash\n# default env variables, corresponding to http://127.0.0.1:8181/v1/data/cats/allow\ncp example.env .env\n\n# development\n$ npm run start\n\n# watch mode\n$ npm run start:dev\n\n# production mode\n$ npm run start:prod\n```\n\n## Start OPA with the example policies\n\n```bash\nopa run --server policies\n```\n\n## Run some HTTP calls\n\n```bash\nhurl basic.hurl\n```\n\n(`hurl` is https://hurl.dev)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FStyraOSS%2Fopa-typescript-example-nestjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FStyraOSS%2Fopa-typescript-example-nestjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FStyraOSS%2Fopa-typescript-example-nestjs/lists"}