Skip to content

Commit d972aad

Browse files
committed
TW-1733 Minor refactoring
1 parent 7c1be3b commit d972aad

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/routers/google-drive.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ const googleDriveApi = axios.create({
1414
baseURL: 'https://www.googleapis.com'
1515
});
1616

17-
type AllowedBodyMethod = 'post' | 'patch';
17+
class NotAllowedMethodError extends Error {
18+
constructor(message: string) {
19+
super(message);
20+
this.name = 'NotAllowedMethodError';
21+
}
22+
}
23+
1824
const allowedBodyMethods = ['post', 'patch'];
19-
const isAllowedBodyMethod = (method: string): method is AllowedBodyMethod => allowedBodyMethods.includes(method);
20-
type AllowedNoBodyMethod = 'get' | 'delete';
25+
const isAllowedBodyMethod = (method: string): method is 'post' | 'patch' => allowedBodyMethods.includes(method);
2126
const allowedNoBodyMethods = ['get', 'delete'];
22-
const isAllowedNoBodyMethod = (method: string): method is AllowedNoBodyMethod => allowedNoBodyMethods.includes(method);
27+
const isAllowedNoBodyMethod = (method: string): method is 'get' | 'delete' => allowedNoBodyMethods.includes(method);
2328

2429
const toAxiosRequestHeaders = (headers: IncomingHttpHeaders): AxiosRequestHeaders => {
2530
const axiosHeaders: AxiosRequestHeaders = {};
@@ -74,13 +79,14 @@ googleDriveRouter.use(async (req, res) => {
7479
} catch {}
7580
response = await googleDriveApi[methodName](req.path, body, requestConfig);
7681
} else {
77-
throw new Error('Method Not Allowed');
82+
throw new NotAllowedMethodError('Method Not Allowed');
7883
}
7984

8085
res.status(response.status).setHeaders(fromAxiosResponseHeaders(response.headers)).send(response.data);
8186
} catch (error) {
8287
logger.error('Google Drive API error', error);
83-
if (!isAllowedNoBodyMethod(methodName) && !isAllowedBodyMethod(methodName)) {
88+
89+
if (error instanceof NotAllowedMethodError) {
8490
return res.status(405).json({ error: 'Method Not Allowed' });
8591
}
8692

0 commit comments

Comments
 (0)