{"id":15647491,"url":"https://github.com/vh13294/nestjs-url-generator","last_synced_at":"2025-10-30T08:32:19.993Z","repository":{"id":44546084,"uuid":"307948259","full_name":"vh13294/nestjs-url-generator","owner":"vh13294","description":"Dynamically generating \u0026 signing URL based on controller method reference.","archived":false,"fork":false,"pushed_at":"2024-01-29T12:12:04.000Z","size":501,"stargazers_count":47,"open_issues_count":5,"forks_count":18,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-26T06:46:59.719Z","etag":null,"topics":["nestjs","sign-urls","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vh13294.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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,"publiccode":null,"codemeta":null}},"created_at":"2020-10-28T08:15:01.000Z","updated_at":"2025-01-15T16:41:11.000Z","dependencies_parsed_at":"2024-06-19T20:04:00.185Z","dependency_job_id":"0db62d27-c453-4166-85da-9d49611c3719","html_url":"https://github.com/vh13294/nestjs-url-generator","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vh13294%2Fnestjs-url-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vh13294%2Fnestjs-url-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vh13294%2Fnestjs-url-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vh13294%2Fnestjs-url-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vh13294","download_url":"https://codeload.github.com/vh13294/nestjs-url-generator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248886327,"owners_count":21177644,"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":["nestjs","sign-urls","typescript"],"created_at":"2024-10-03T12:19:38.966Z","updated_at":"2025-10-30T08:32:19.881Z","avatar_url":"https://github.com/vh13294.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003cimg width=\"40%\" src=\"https://user-images.githubusercontent.com/17086745/97110220-8bf2f780-170a-11eb-9bf4-ca38b8d41be9.png\" /\u003e\n\u003c/p\u003e\n\n\u003ch2 align=\"center\"\u003eNestJS module for generating \u0026 signing URL\u003c/h2\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://www.codefactor.io/repository/github/vh13294/nestjs-url-generator\"\u003e\n        \u003cimg src=\"https://www.codefactor.io/repository/github/vh13294/nestjs-url-generator/badge\" alt=\"CodeFactor\" /\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://www.npmjs.com/package/nestjs-url-generator\"\u003e\n        \u003cimg src=\"https://img.shields.io/npm/v/nestjs-url-generator.svg?style=flat-square\u0026sanitize=true\" alt=\"NPM Version\" /\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://www.npmjs.com/package/nestjs-url-generator\"\u003e\n        \u003cimg src=\"https://img.shields.io/npm/dm/nestjs-url-generator.svg?style=flat-square\u0026sanitize=true\" alt=\"NPM Downloads\" /\u003e\n    \u003c/a\u003e\n    \u003ca href=\"#\"\u003e\n        \u003cimg src=\"https://img.shields.io/npm/l/nestjs-url-generator.svg?colorB=black\u0026label=LICENSE\u0026style=flat-square\u0026sanitize=true\" alt=\"License\"/\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n# Description\n\nURL Generation is used to dynamically generate URL that point to NestJS controller method (Route).\n\nnestjs-url-generator can generate plain and signed URLs\n\n# Installation\n\n```bash\nnpm i --save nestjs-url-generator\n```\n\nOr if you use Yarn:\n\n```bash\nyarn add nestjs-url-generator\n```\n\n# Requirements\n\n`nestjs-url-generator` is built to work with Nest 7 and newer versions.\n\n# Basic Usage\n\n### Include Module\n\nFirst you need to import [UrlGeneratorModule]:\n\n\u003e app.module.ts\n\n```ts\nimport { UrlGeneratorModule } from 'nestjs-url-generator';\n\n@Module({\n  imports: [\n    UrlGeneratorModule.forRoot({\n      secret: 'secret', // optional, required only for signed URL\n      appUrl: 'https://localhost:3000',\n    }),\n  ],\n})\nexport class ApplicationModule {}\n```\n\nOr Async Import With .ENV usage\n\n\u003e .ENV\n\n```.env\nAPP_KEY=secret\nAPP_URL=https://localhost:3000\n```\n\n\u003e signed-url.config.ts\n\n```ts\nimport { UrlGeneratorModuleOptions } from 'nestjs-url-generator';\n\nexport function urlGeneratorModuleConfig(): UrlGeneratorModuleOptions {\n  return {\n    secret: process.env.APP_KEY,\n    appUrl: process.env.APP_URL,\n  };\n}\n```\n\n\u003e app.module.ts\n\n```ts\nimport { UrlGeneratorModule } from 'nestjs-url-generator';\n\n@Module({\n  imports: [\n    ConfigModule.forRoot(),\n    UrlGeneratorModule.forRootAsync({\n      useFactory: () =\u003e urlGeneratorModuleConfig(),\n    }),\n  ],\n})\nexport class ApplicationModule {}\n```\n\n## Using Service\n\nNow you need to register the service, by injecting it to the constructor.\nThere are two methods for generating url:\n\n```typescript\ngenerateUrlFromController({\n  controller,\n  controllerMethod,\n  /*?*/ query,\n  /*?*/ params,\n});\n\ngenerateUrlFromPath({\n  relativePath,\n  /*?*/ query,\n  /*?*/ params,\n});\n```\n\n\u003e app.controller.ts\n\n```ts\nimport { UrlGeneratorService } from 'nestjs-url-generator';\n\n@Controller()\nexport class AppController {\n  constructor(private readonly urlGeneratorService: UrlGeneratorService) {}\n\n  @Get('makeUrl')\n  async makeUrl(): Promise\u003cstring\u003e {\n    const params = {\n      version: '1.0',\n      userId: 12,\n    };\n\n    const query = {\n      email: 'email@email',\n    };\n\n    // This will generate:\n    // https://localhost:3000/emailVerification/1.0/12?email=email%40email\n    return this.urlGeneratorService.generateUrlFromController({\n      controller: AppController,\n      controllerMethod: AppController.prototype.emailVerification,\n      query: query,\n      params: params,\n    });\n  }\n}\n```\n\n### Generate Signed URL\n\nThere are two methods for generating url:\n\n```typescript\nSignControllerUrl({\n  controller,\n  controllerMethod,\n  /*?*/ expirationDate,\n  /*?*/ query,\n  /*?*/ params,\n});\n\nSignUrl({\n  relativePath,\n  /*?*/ expirationDate,\n  /*?*/ query,\n  /*?*/ params,\n});\n```\n\n\u003e app.controller.ts\n\n```ts\nimport { UrlGeneratorService } from 'nestjs-url-generator';\n\n@Controller()\nexport class AppController {\n  constructor(private readonly urlGeneratorService: UrlGeneratorService) {}\n\n  @Get('makeSignUrl')\n  async makeSignUrl(): Promise\u003cstring\u003e {\n    // This will generate:\n    // https://localhost:3000/emailVerification?\n    // expirationDate=2021-12-12T00%3A00%3A00.000Z\u0026\n    // signed=84b5a021c433d0ee961932ac0ec04d5dd5ffd6f7fdb60b46083cfe474dfae3c0\n    return this.urlGeneratorService.SignControllerUrl({\n      controller: AppController,\n      controllerMethod: AppController.prototype.emailVerification,\n      expirationDate: new Date('2021-12-12'),\n      // or using DateTime library of your choice\n      // will be expired 30 minutes after it was created\n      expirationDate: dayjs().add(30, 'minute').toDate(),\n    });\n  }\n}\n```\n\n- [expirationDate] and [signed] query keys are used for signed URL.\n\n- By default, the signed URLs lives forever.\n  You can add expiration date to them at the time of generating one.\n\n### Reminder\n\nThe difference between params \u0026 query in ExpressJS\n\n\u003cimg width=\"50%\" src=\"https://user-images.githubusercontent.com/17086745/97456127-22bdef00-196b-11eb-89d2-d64d4324498d.png\" /\u003e\n\u003cbr\u003e\n\n## Using Guard\n\nYou can use SignUrlGuard to verify the signed url in controller.\n\nIf the url has been tampered or when the expiration date is due, then a Forbidden exception will be thrown.\n\n\u003e app.controller.ts\n\n```ts\nimport { SignedUrlGuard } from 'nestjs-url-generator';\n\n@Controller()\nexport class AppController {\n  constructor(private readonly urlGeneratorService: UrlGeneratorService) {}\n\n  @Get('emailVerification')\n  @UseGuards(SignedUrlGuard)\n  async emailVerification(): Promise\u003cstring\u003e {\n    return 'You emailed has been verified.';\n  }\n}\n```\n\n## Note\n\n- Changing the secret key will invalidate all signed urls\n- Signed URL is typically used for unsubscribe email, email verification, sign file permission, and more.\n\n- If you are using https with reverse proxy please make sure to enable trust proxy in express\n\n```typescript\nconst app = await NestFactory.create\u003cNestExpressApplication\u003e(AppModule);\n\napp.set('trust proxy', true);\n// or\nexpressSession({ proxy: true });\n```\n\n## Generating Keys using node REPL\n\n```javascript\nrequire('crypto').randomBytes(64, (err, buf) =\u003e {\n  if (err) throw err;\n  console.log(`${buf.length} bytes of random data: ${buf.toString('base64')}`);\n  process.exit();\n});\n```\n\n### TODO\n\n- [ ] Create unit test (expiration, tampered, with or without globalPrefix, request with or without query \u0026 param, if target for signerUrl doesn't have guard)\n\n- [ ] Automate CI, npm run build, push, npm publish\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvh13294%2Fnestjs-url-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvh13294%2Fnestjs-url-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvh13294%2Fnestjs-url-generator/lists"}