Files
twenty/packages/twenty-server/src/engine/api/rest/rest-api.service.ts
T
Félix Malfait 8b4b9ef8da Change type import rule (#13751)
Forcing "type" to be explicit, works best will rollup on the frontend to
exclude depdendencies
2025-08-08 01:27:05 +02:00

49 lines
1.3 KiB
TypeScript

import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import { type AxiosResponse } from 'axios';
import { type Query } from 'src/engine/api/rest/core/types/query.type';
import { RestApiException } from 'src/engine/api/rest/errors/RestApiException';
import { type RequestContext } from 'src/engine/api/rest/types/RequestContext';
export enum GraphqlApiType {
CORE = 'core',
METADATA = 'metadata',
}
@Injectable()
export class RestApiService {
constructor(private readonly httpService: HttpService) {}
async call(
graphqlApiType: GraphqlApiType,
requestContext: RequestContext,
data: Query,
) {
let response: AxiosResponse;
const url = `${requestContext.baseUrl}/${
graphqlApiType === GraphqlApiType.CORE
? 'graphql'
: GraphqlApiType.METADATA
}`;
try {
response = await this.httpService.axiosRef.post(url, data, {
headers: {
'Content-Type': 'application/json',
Authorization: requestContext.headers.authorization,
},
});
} catch (err) {
throw new RestApiException(err.response.data.errors);
}
if (response.data.errors?.length) {
throw new RestApiException(response.data.errors);
}
return response;
}
}