MCPcopy Create free account
hub / github.com/apitable/apitable / initHttpHook

Function initHttpHook

packages/room-server/src/shared/adapters/adapters.init.ts:141–192  ·  view source on GitHub ↗
(app: INestApplication)

Source from the content-addressed store, hash-verified

139};
140
141export const initHttpHook = (app: INestApplication) => {
142 const fastify = app.getHttpAdapter().getInstance() as FastifyInstance;
143 fastify.decorateRequest(USER_HTTP_DECORATE, null);
144 fastify.decorateRequest(DATASHEET_META_HTTP_DECORATE, null);
145 fastify.decorateRequest(DATASHEET_LINKED, null);
146 fastify.decorateRequest(DATASHEET_ENRICH_SELECT_FIELD, null);
147 fastify.decorateRequest(DATASHEET_MEMBER_FIELD, null);
148 // TODO: REQUEST_ID should be returned by api-gateway, so that we could trace all the services
149 // TODO: support multiple languages
150 fastify.decorateRequest(REQUEST_ID, null);
151 fastify.decorateRequest(REQUEST_AT, null);
152
153 fastify.addHook('preHandler', async (request) => {
154 request[REQUEST_AT] = Date.now();
155 request[REQUEST_ID] = generateRandomString();
156 if (request.headers.authorization && request.headers.authorization.startsWith(AUTHORIZATION_PREFIX)) {
157 const developerService = app.select(DatabaseModule).get(DeveloperService);
158 const apiKey = request.headers.authorization.slice(AUTHORIZATION_PREFIX.length);
159 request[USER_HTTP_DECORATE] = await developerService.getUserInfoByApiKey(apiKey);
160 }
161 if ((request.params as any)['spaceId']) {
162 request[SPACE_ID_HTTP_DECORATE] = (request.params as any)['spaceId'];
163 }
164 if ((request.params as any)['nodeId']) {
165 const nodeRepository = app.select(DatabaseModule).get(NodeRepository);
166 const nodeInfo = await nodeRepository.getNodeInfo((request.params as any)['nodeId']);
167 if (nodeInfo) {
168 request[NODE_INFO] = nodeInfo;
169 request[SPACE_ID_HTTP_DECORATE] = nodeInfo.spaceId;
170 }
171 }
172 if (request.body && request.body['folderId']) {
173 const nodeRepository = app.select(DatabaseModule).get(NodeRepository);
174 const folderId = request.body['folderId'];
175 request[REQUEST_HOOK_FOLDER] = await nodeRepository.getNodeInfo(folderId);
176 }
177 if (request.body && request.body['preNodeId']) {
178 const nodeRepository = app.select(DatabaseModule).get(NodeRepository);
179 const preNodeId = request.body['preNodeId'];
180 request[REQUEST_HOOK_PRE_NODE] = await nodeRepository.getNodeInfo(preNodeId);
181 }
182 return;
183 });
184 fastify.addHook('onSend', (request, reply, _payload, done) => {
185 // add request-id to Headers
186 // TODO: REQUEST_ID should be returned by api-gateway, so that we could trace all the services
187 void reply.header(REQUEST_ID, request[REQUEST_ID]);
188 const serverTime = Date.now() - request[REQUEST_AT];
189 void reply.header(SERVER_TIME, 'total;dur=' + serverTime);
190 done();
191 });
192};
193
194export const initSentry = (_app: INestApplication) => {
195 const sentryDsn = process.env.SENTRY_DSN;

Callers 2

fusion.e2e-spec.tsFile · 0.90
bootstrapFunction · 0.90

Calls 5

generateRandomStringFunction · 0.90
getUserInfoByApiKeyMethod · 0.80
getMethod · 0.65
selectMethod · 0.65
getNodeInfoMethod · 0.65

Tested by

no test coverage detected