| 1 | import { |
| 2 | type CallHandler, |
| 3 | type ExecutionContext, |
| 4 | Injectable, |
| 5 | type NestInterceptor, |
| 6 | } from '@nestjs/common'; |
| 7 | import { HTTP_CODE_METADATA } |
| 8 | import { ConfigService } |
| 9 | import { Reflector } |
| 10 | import type { Request, |
| 11 | import { map } |
| 12 | |
| 13 | import { githubOrigins } |
| 14 | import { linkHeader, RestPage |
| 15 | |
| 16 | /** What every GitHub REST response carries beyond its body: the `Link` header of a paged list, and the `Location` of what a 201 created. */ |
| 17 | @Injectable() |
| 18 | export class GithubRestInterceptor |
| 19 | constructor( |
| 20 | private readonly config |
| 21 | private readonly reflector |
| 22 | ) {} |
| 23 | |
| 24 | intercept(context: |
| 25 | const http = |
| 26 | const request = |
| 27 | const response = |
| 28 | const created = |
| 29 | request.method === |
| 30 | (this.reflector.get |
| 31 | 201) === 201; |
| 32 | return next.handle |
| 33 | map((result: |
| 34 | if (result instanceof |
| 35 | const url = |
| 36 | request.originalUrl, |
| 37 | githubOrigins(this.config |
| 38 | ); |
| 39 | const link = |
| 40 | if (link) response |
| 41 | return result.items |
| 42 | } |
| 43 | if ( |
| 44 | created && |
| 45 | typeof result === ' |
| 46 | result !== null && |
| 47 | 'url' in result |
| 48 | typeof result.url |
| 49 | ) |
| 50 | response.setHeader('Location |
| 51 | return result; |
| 52 | }), |
| 53 | ); |
| 54 | } |
| 55 | } |