mirror of
https://github.com/twentyhq/twenty.git
synced 2024-12-26 05:24:04 +03:00
Fix api rest (#2860)
* Throw an error if workspace id has no object * Request only plurial object names * Fix tests * Fix query * Handle graphql errors * Fix comment
This commit is contained in:
parent
62fa55eae6
commit
4fecf6d8b9
@ -1,4 +1,8 @@
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
|
||||
import { Request } from 'express';
|
||||
|
||||
@ -40,7 +44,15 @@ export class ApiRestQueryBuilderFactory {
|
||||
objectMetadataItems: ObjectMetadataEntity[];
|
||||
objectMetadataItem: ObjectMetadataEntity;
|
||||
}> {
|
||||
const workspaceId = await this.tokenService.verifyApiKeyToken(request);
|
||||
let workspaceId;
|
||||
|
||||
try {
|
||||
workspaceId = await this.tokenService.verifyApiKeyToken(request);
|
||||
} catch (err) {
|
||||
throw new UnauthorizedException(
|
||||
`Invalid API key. Double check your API key or generate a new one here ${this.environmentService.getFrontBaseUrl()}/settings/developers/api-keys`,
|
||||
);
|
||||
}
|
||||
|
||||
const objectMetadataItems =
|
||||
await this.objectMetadataService.findManyWithinWorkspace(workspaceId);
|
||||
|
@ -7,7 +7,9 @@ import { ApiRestResponse } from 'src/core/api-rest/types/api-rest-response.type'
|
||||
|
||||
const handleResult = (res: Response, result: ApiRestResponse) => {
|
||||
if (result.data.error) {
|
||||
res.status(result.data.status || 400).send({ error: result.data.error });
|
||||
res
|
||||
.status(result.data.status || 400)
|
||||
.send({ error: `${result.data.error}` });
|
||||
} else {
|
||||
res.send(result.data);
|
||||
}
|
||||
|
@ -25,11 +25,20 @@ export class ApiRestService {
|
||||
this.environmentService.getServerUrl() ||
|
||||
`${request.protocol}://${request.get('host')}`;
|
||||
|
||||
return await axios.post(`${baseUrl}/graphql`, data, {
|
||||
headers: {
|
||||
Authorization: request.headers.authorization,
|
||||
},
|
||||
});
|
||||
try {
|
||||
return await axios.post(`${baseUrl}/graphql`, data, {
|
||||
headers: {
|
||||
Authorization: request.headers.authorization,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
return {
|
||||
data: {
|
||||
error: `AxiosError: please double check your query and your API key (to generate a new one, see here: ${this.environmentService.getFrontBaseUrl()}/settings/developers/api-keys)`,
|
||||
status: 400,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async get(request: Request): Promise<ApiRestResponse> {
|
||||
@ -38,7 +47,7 @@ export class ApiRestService {
|
||||
|
||||
return await this.callGraphql(request, data);
|
||||
} catch (err) {
|
||||
return { data: { error: `${err}`, status: err.response.status } };
|
||||
return { data: { error: err, status: err.status } };
|
||||
}
|
||||
}
|
||||
|
||||
@ -48,7 +57,7 @@ export class ApiRestService {
|
||||
|
||||
return await this.callGraphql(request, data);
|
||||
} catch (err) {
|
||||
return { data: { error: `${err}` } };
|
||||
return { data: { error: err, status: err.status } };
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,7 +67,7 @@ export class ApiRestService {
|
||||
|
||||
return await this.callGraphql(request, data);
|
||||
} catch (err) {
|
||||
return { data: { error: `${err}` } };
|
||||
return { data: { error: err, status: err.status } };
|
||||
}
|
||||
}
|
||||
|
||||
@ -68,7 +77,7 @@ export class ApiRestService {
|
||||
|
||||
return await this.callGraphql(request, data);
|
||||
} catch (err) {
|
||||
return { data: { error: `${err}` } };
|
||||
return { data: { error: err, status: err.status } };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1 +1,5 @@
|
||||
export type ApiRestResponse = { data: { error?: string; status?: number } };
|
||||
import { HttpException } from '@nestjs/common';
|
||||
|
||||
export type ApiRestResponse = {
|
||||
data: { error?: HttpException | string; status?: number };
|
||||
};
|
||||
|
@ -60,7 +60,7 @@ export class EnvironmentVariables {
|
||||
@IsUrl({ require_tld: false })
|
||||
FRONT_BASE_URL: string;
|
||||
|
||||
// Frontend URL
|
||||
// Server URL
|
||||
@IsUrl({ require_tld: false })
|
||||
@IsOptional()
|
||||
SERVER_URL: string;
|
||||
|
Loading…
Reference in New Issue
Block a user