-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
716 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { FastifyReply, FastifyRequest } from "fastify"; | ||
import { | ||
deleteUrlData, | ||
getUrlData, | ||
patchUrlData, | ||
postUrlData, | ||
putUrlData, | ||
} from "./url.service"; | ||
import { TUrlQuery, TErrorResponse, TUrlBody } from "./url.schema"; | ||
import { AxiosError } from "axios"; | ||
|
||
const errorHandler = (err: unknown): Omit<TErrorResponse, "path"> => { | ||
if (err instanceof AxiosError) | ||
return { | ||
message: err.message, | ||
code: err.code, | ||
status: err.response?.status, | ||
}; | ||
else { | ||
return { | ||
message: "Unknown error", | ||
code: "ERROR", | ||
status: 500, | ||
}; | ||
} | ||
}; | ||
|
||
const getUrlHandler = async ( | ||
request: FastifyRequest<{ Querystring: TUrlQuery }>, | ||
reply: FastifyReply | ||
) => { | ||
const { link } = request.query; | ||
try { | ||
const response = await getUrlData(link); | ||
reply.status(200).send(response.data); | ||
} catch (error) { | ||
const errHandler = errorHandler(error); | ||
reply.status(errHandler.status || 500).send({ | ||
...errHandler, | ||
path: link, | ||
}); | ||
} | ||
}; | ||
|
||
const deletetUrlHandler = async ( | ||
request: FastifyRequest<{ Querystring: TUrlQuery }>, | ||
reply: FastifyReply | ||
) => { | ||
const { link } = request.query; | ||
try { | ||
const response = await deleteUrlData(link); | ||
reply.status(200).send(response.data); | ||
} catch (error) { | ||
const errHandler = errorHandler(error); | ||
reply.status(errHandler.status || 500).send({ | ||
...errHandler, | ||
path: link, | ||
}); | ||
} | ||
}; | ||
|
||
const postUrlHandler = async ( | ||
request: FastifyRequest<{ Querystring: TUrlQuery; Body: TUrlBody }>, | ||
reply: FastifyReply | ||
) => { | ||
const { link } = request.query; | ||
const data = request.body; | ||
|
||
try { | ||
const response = await postUrlData(link, data); | ||
reply.status(response.status).send(response.data); | ||
} catch (error) { | ||
const errHandler = errorHandler(error); | ||
reply.status(errHandler.status || 500).send({ | ||
...errHandler, | ||
path: link, | ||
}); | ||
} | ||
}; | ||
|
||
const patchUrlHandler = async ( | ||
request: FastifyRequest<{ Querystring: TUrlQuery; Body: TUrlBody }>, | ||
reply: FastifyReply | ||
) => { | ||
const { link } = request.query; | ||
const data = request.body; | ||
try { | ||
const response = await patchUrlData(link, data); | ||
reply.status(response.status).send(response.data); | ||
} catch (error) { | ||
const errHandler = errorHandler(error); | ||
reply.status(errHandler.status || 500).send({ | ||
...errHandler, | ||
path: link, | ||
}); | ||
} | ||
}; | ||
|
||
const putUrlHandler = async ( | ||
request: FastifyRequest<{ Querystring: TUrlQuery; Body: TUrlBody }>, | ||
reply: FastifyReply | ||
) => { | ||
const { link } = request.query; | ||
const data = request.body; | ||
|
||
try { | ||
const response = await putUrlData(link, data); | ||
reply.status(response.status).send(response.data); | ||
} catch (error) { | ||
const errHandler = errorHandler(error); | ||
reply.status(errHandler.status || 500).send({ | ||
...errHandler, | ||
path: link, | ||
}); | ||
} | ||
}; | ||
|
||
export { | ||
getUrlHandler, | ||
postUrlHandler, | ||
deletetUrlHandler, | ||
putUrlHandler, | ||
patchUrlHandler, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { FastifyInstance } from "fastify"; | ||
import { | ||
getUrlHandler, | ||
postUrlHandler, | ||
deletetUrlHandler, | ||
putUrlHandler, | ||
patchUrlHandler, | ||
} from "./url.controller"; | ||
import { | ||
urlBodySchemaJson, | ||
urlQuerySchemaJson, | ||
urlResponseRerrorSchemaJson, | ||
urlResponseSchemaJson, | ||
} from "./url.schema"; | ||
|
||
const urlRoutes = async (server: FastifyInstance) => { | ||
server.get( | ||
"/", | ||
{ | ||
schema: { | ||
querystring: urlQuerySchemaJson, | ||
response: { | ||
"2xx": urlResponseSchemaJson, | ||
"4xx": urlResponseRerrorSchemaJson, | ||
"5xx": urlResponseRerrorSchemaJson, | ||
}, | ||
}, | ||
}, | ||
getUrlHandler | ||
); | ||
server.post( | ||
"/", | ||
{ | ||
schema: { | ||
querystring: urlQuerySchemaJson, | ||
body: urlBodySchemaJson, | ||
response: { | ||
"2xx": urlResponseSchemaJson, | ||
"4xx": urlResponseRerrorSchemaJson, | ||
"5xx": urlResponseRerrorSchemaJson, | ||
}, | ||
}, | ||
}, | ||
postUrlHandler | ||
); | ||
server.delete( | ||
"/", | ||
{ | ||
schema: { | ||
querystring: urlQuerySchemaJson, | ||
response: { | ||
"2xx": urlResponseSchemaJson, | ||
"4xx": urlResponseRerrorSchemaJson, | ||
"5xx": urlResponseRerrorSchemaJson, | ||
}, | ||
}, | ||
}, | ||
deletetUrlHandler | ||
); | ||
server.put( | ||
"/", | ||
{ | ||
schema: { | ||
querystring: urlQuerySchemaJson, | ||
body: urlBodySchemaJson, | ||
response: { | ||
"2xx": urlResponseSchemaJson, | ||
"4xx": urlResponseRerrorSchemaJson, | ||
"5xx": urlResponseRerrorSchemaJson, | ||
}, | ||
}, | ||
}, | ||
putUrlHandler | ||
); | ||
server.patch( | ||
"/", | ||
{ | ||
schema: { | ||
querystring: urlQuerySchemaJson, | ||
body: urlBodySchemaJson, | ||
response: { | ||
"2xx": urlResponseSchemaJson, | ||
"4xx": urlResponseRerrorSchemaJson, | ||
"5xx": urlResponseRerrorSchemaJson, | ||
}, | ||
}, | ||
}, | ||
patchUrlHandler | ||
); | ||
}; | ||
export default urlRoutes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { z } from "zod"; | ||
import zodToJsonSchema from "zod-to-json-schema"; | ||
|
||
export const urlQuerySchema = z.object({ | ||
link: z.string().refine( | ||
(val) => { | ||
try { | ||
new URL(val); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
}, | ||
{ message: "Invalid URL format" } | ||
), | ||
}); | ||
|
||
const urlBodySchema = z.record(z.any()); | ||
const urlResponseErrorSchema = z.object({ | ||
message: z.string().optional(), | ||
status: z.number().optional(), | ||
code: z.string().optional(), | ||
path: z.string().url(), | ||
}); | ||
const urlResponseSchema = z.any(); | ||
|
||
const urlResponseRerrorSchemaJson = zodToJsonSchema(urlResponseErrorSchema); | ||
const urlQuerySchemaJson = zodToJsonSchema(urlQuerySchema); | ||
const urlResponseSchemaJson = zodToJsonSchema(urlResponseSchema); | ||
const urlBodySchemaJson = zodToJsonSchema(urlBodySchema); | ||
|
||
type TUrlQuery = z.infer<typeof urlQuerySchema>; | ||
type TErrorResponse = z.infer<typeof urlResponseErrorSchema>; | ||
type TUrlBody = z.infer<typeof urlBodySchema>; | ||
|
||
export { | ||
urlQuerySchemaJson, | ||
urlResponseSchemaJson, | ||
urlBodySchemaJson, | ||
urlResponseRerrorSchemaJson, | ||
}; | ||
export type { TUrlQuery, TErrorResponse, TUrlBody }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import axios from "axios"; | ||
import { TJsonable } from "../../types/jsonable.types"; | ||
|
||
const getUrlData = async (link: string) => { | ||
const response = await axios.get(link); | ||
return response; | ||
}; | ||
|
||
const deleteUrlData = async (link: string) => { | ||
const response = await axios.delete(link); | ||
return response; | ||
}; | ||
|
||
const postUrlData = async (link: string, body: TJsonable) => { | ||
const response = await axios.post(link, body); | ||
return response; | ||
}; | ||
|
||
const putUrlData = async (link: string, body: TJsonable) => { | ||
const response = await axios.put(link, body); | ||
return response; | ||
}; | ||
|
||
const patchUrlData = async (link: string, body: TJsonable) => { | ||
const response = await axios.patch(link, body); | ||
return response; | ||
}; | ||
|
||
export { getUrlData, postUrlData, patchUrlData, putUrlData, deleteUrlData }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { FastifyPluginCallback } from "fastify"; | ||
import FastifySwagger from "@fastify/swagger"; | ||
import FastifySwaggerUi from "@fastify/swagger-ui"; | ||
|
||
const swagger: FastifyPluginCallback = async (fastify, _, done) => { | ||
await fastify.register(FastifySwagger, { | ||
openapi: { | ||
info: { | ||
title: "CRUD PROXY", | ||
version: "1.0.0", | ||
}, | ||
}, | ||
}); | ||
|
||
await fastify.register(FastifySwaggerUi, { | ||
routePrefix: "/docs", | ||
}); | ||
|
||
done(); | ||
}; | ||
|
||
export default swagger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
type TJsonable = | ||
| string | ||
| number | ||
| boolean | ||
| null | ||
| TJsonable[] | ||
| { [key: string]: TJsonable }; | ||
|
||
export type { TJsonable }; |
Oops, something went wrong.