apps/api: add query-ttl config
proxyController: pass all request headers to proxied server
This commit is contained in:
parent
6041082fec
commit
4bc5234d09
1
apps/api/src/config/constants.ts
Normal file
1
apps/api/src/config/constants.ts
Normal file
@ -0,0 +1 @@
|
||||
export const DEFAULT_CACHE_TTL = 15 * 60;
|
||||
@ -1,10 +1,11 @@
|
||||
import { DEFAULT_CACHE_TTL } from '../constants';
|
||||
import { z } from 'zod';
|
||||
|
||||
const envSchema = z.object({
|
||||
CACHE_TTL: z
|
||||
.string()
|
||||
.transform((val) => Number.parseInt(val, 10))
|
||||
.default('900'),
|
||||
.default(DEFAULT_CACHE_TTL.toString()),
|
||||
PORT: z
|
||||
.string()
|
||||
.transform((val) => Number.parseInt(val, 10))
|
||||
|
||||
3
apps/api/src/proxy/lib/config.ts
Normal file
3
apps/api/src/proxy/lib/config.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export const queryTTL: Record<string, number> = {
|
||||
GetSystemUser: 6 * 60 * 60,
|
||||
};
|
||||
@ -1,13 +1,20 @@
|
||||
import { queryTTL } from './lib/config';
|
||||
import type { GQLRequest } from './types';
|
||||
import { CACHE_MANAGER } from '@nestjs/cache-manager';
|
||||
import { All, Controller, Inject, Req, Res } from '@nestjs/common';
|
||||
import { Cache } from 'cache-manager';
|
||||
import type { Cache } from 'cache-manager';
|
||||
import { FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { env } from 'src/config/env';
|
||||
|
||||
type RedisStore = Omit<Cache, 'set'> & {
|
||||
set: (key: string, value: unknown, { ttl }: { ttl: number }) => Promise<void>;
|
||||
};
|
||||
|
||||
@Controller('proxy')
|
||||
export class ProxyController {
|
||||
constructor(@Inject(CACHE_MANAGER) private readonly cacheManager: Cache) {}
|
||||
constructor(
|
||||
@Inject(CACHE_MANAGER) private readonly cacheManager: RedisStore,
|
||||
) {}
|
||||
@All('/graphql')
|
||||
public async graphql(@Req() req: FastifyRequest, @Res() reply: FastifyReply) {
|
||||
const { operationName, query, variables } = req.body as GQLRequest;
|
||||
@ -15,22 +22,19 @@ export class ProxyController {
|
||||
const key = `${operationName} ${JSON.stringify(variables)}`;
|
||||
const cached = await this.cacheManager.get(key);
|
||||
|
||||
if (cached) {
|
||||
return reply.send(cached);
|
||||
}
|
||||
if (cached) return reply.send(cached);
|
||||
|
||||
const response = await fetch(env.URL_CRM_GRAPHQL_DIRECT, {
|
||||
body: JSON.stringify({ operationName, query, variables }),
|
||||
headers: {
|
||||
Authorization: req.headers.authorization,
|
||||
'Content-Type': 'application/json',
|
||||
Cookie: req.headers.cookie,
|
||||
},
|
||||
headers: req.headers as HeadersInit,
|
||||
method: req.method,
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
if (data) await this.cacheManager.set(key, data);
|
||||
if (data) {
|
||||
const ttl = queryTTL[operationName] || env.CACHE_TTL;
|
||||
await this.cacheManager.set(key, data, { ttl });
|
||||
}
|
||||
|
||||
return reply.send(data);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user