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:
@ -1,4 +1,8 @@
|
|||||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
Injectable,
|
||||||
|
UnauthorizedException,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
|
||||||
import { Request } from 'express';
|
import { Request } from 'express';
|
||||||
|
|
||||||
@ -40,7 +44,15 @@ export class ApiRestQueryBuilderFactory {
|
|||||||
objectMetadataItems: ObjectMetadataEntity[];
|
objectMetadataItems: ObjectMetadataEntity[];
|
||||||
objectMetadataItem: 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 =
|
const objectMetadataItems =
|
||||||
await this.objectMetadataService.findManyWithinWorkspace(workspaceId);
|
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) => {
|
const handleResult = (res: Response, result: ApiRestResponse) => {
|
||||||
if (result.data.error) {
|
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 {
|
} else {
|
||||||
res.send(result.data);
|
res.send(result.data);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,11 +25,20 @@ export class ApiRestService {
|
|||||||
this.environmentService.getServerUrl() ||
|
this.environmentService.getServerUrl() ||
|
||||||
`${request.protocol}://${request.get('host')}`;
|
`${request.protocol}://${request.get('host')}`;
|
||||||
|
|
||||||
return await axios.post(`${baseUrl}/graphql`, data, {
|
try {
|
||||||
headers: {
|
return await axios.post(`${baseUrl}/graphql`, data, {
|
||||||
Authorization: request.headers.authorization,
|
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> {
|
async get(request: Request): Promise<ApiRestResponse> {
|
||||||
@ -38,7 +47,7 @@ export class ApiRestService {
|
|||||||
|
|
||||||
return await this.callGraphql(request, data);
|
return await this.callGraphql(request, data);
|
||||||
} catch (err) {
|
} 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);
|
return await this.callGraphql(request, data);
|
||||||
} catch (err) {
|
} 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);
|
return await this.callGraphql(request, data);
|
||||||
} catch (err) {
|
} 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);
|
return await this.callGraphql(request, data);
|
||||||
} catch (err) {
|
} 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 })
|
@IsUrl({ require_tld: false })
|
||||||
FRONT_BASE_URL: string;
|
FRONT_BASE_URL: string;
|
||||||
|
|
||||||
// Frontend URL
|
// Server URL
|
||||||
@IsUrl({ require_tld: false })
|
@IsUrl({ require_tld: false })
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
SERVER_URL: string;
|
SERVER_URL: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user