| 
 | 1 | +import { FastifyPluginAsync } from "fastify";  | 
 | 2 | +import { z } from "zod";  | 
 | 3 | +import { AppRoles } from "../../common/roles.js";  | 
 | 4 | +import { NotImplementedError } from "../../common/errors/index.js";  | 
 | 5 | +import { intersection } from "../plugins/auth.js";  | 
 | 6 | +import { NoDataRequest } from "../types.js";  | 
 | 7 | + | 
 | 8 | +type LinkrySlugOnlyRequest = {  | 
 | 9 | +  Params: { id: string };  | 
 | 10 | +  Querystring: undefined;  | 
 | 11 | +  Body: undefined;  | 
 | 12 | +};  | 
 | 13 | + | 
 | 14 | +const rawRequest = {  | 
 | 15 | +  slug: z.string().min(1),  | 
 | 16 | +  full: z.string().url().min(1),  | 
 | 17 | +  groups: z.optional(z.array(z.string()).min(1)),  | 
 | 18 | +};  | 
 | 19 | + | 
 | 20 | +const createRequest = z.object(rawRequest);  | 
 | 21 | +const patchRequest = z.object({ ...rawRequest, slug: z.undefined() });  | 
 | 22 | + | 
 | 23 | +type LinkyCreateRequest = {  | 
 | 24 | +  Params: undefined;  | 
 | 25 | +  Querystring: undefined;  | 
 | 26 | +  Body: z.infer<typeof createRequest>;  | 
 | 27 | +};  | 
 | 28 | + | 
 | 29 | +type LinkryPatchRequest = {  | 
 | 30 | +  Params: { id: string };  | 
 | 31 | +  Querystring: undefined;  | 
 | 32 | +  Body: z.infer<typeof patchRequest>;  | 
 | 33 | +};  | 
 | 34 | + | 
 | 35 | +const linkryRoutes: FastifyPluginAsync = async (fastify, _options) => {  | 
 | 36 | +  fastify.get<LinkrySlugOnlyRequest>("/redir/:id", async (request, reply) => {  | 
 | 37 | +    throw new NotImplementedError({});  | 
 | 38 | +  });  | 
 | 39 | +  fastify.post<LinkyCreateRequest>(  | 
 | 40 | +    "/redir",  | 
 | 41 | +    {  | 
 | 42 | +      preValidation: async (request, reply) => {  | 
 | 43 | +        await fastify.zodValidateBody(request, reply, createRequest);  | 
 | 44 | +      },  | 
 | 45 | +      onRequest: async (request, reply) => {  | 
 | 46 | +        await fastify.authorize(request, reply, [  | 
 | 47 | +          AppRoles.LINKS_MANAGER,  | 
 | 48 | +          AppRoles.LINKS_ADMIN,  | 
 | 49 | +        ]);  | 
 | 50 | +      },  | 
 | 51 | +    },  | 
 | 52 | +    async (request, reply) => {  | 
 | 53 | +      throw new NotImplementedError({});  | 
 | 54 | +    },  | 
 | 55 | +  );  | 
 | 56 | +  fastify.patch<LinkryPatchRequest>(  | 
 | 57 | +    "/redir/:id",  | 
 | 58 | +    {  | 
 | 59 | +      preValidation: async (request, reply) => {  | 
 | 60 | +        await fastify.zodValidateBody(request, reply, patchRequest);  | 
 | 61 | +      },  | 
 | 62 | +      onRequest: async (request, reply) => {  | 
 | 63 | +        await fastify.authorize(request, reply, [  | 
 | 64 | +          AppRoles.LINKS_MANAGER,  | 
 | 65 | +          AppRoles.LINKS_ADMIN,  | 
 | 66 | +        ]);  | 
 | 67 | +      },  | 
 | 68 | +    },  | 
 | 69 | +    async (request, reply) => {  | 
 | 70 | +      // make sure that a user can manage this link, either via owning or being in a group that has access to it, or is a LINKS_ADMIN.  | 
 | 71 | +      throw new NotImplementedError({});  | 
 | 72 | +    },  | 
 | 73 | +  );  | 
 | 74 | +  fastify.delete<LinkrySlugOnlyRequest>(  | 
 | 75 | +    "/redir/:id",  | 
 | 76 | +    {  | 
 | 77 | +      preValidation: async (request, reply) => {  | 
 | 78 | +        await fastify.zodValidateBody(request, reply, createRequest);  | 
 | 79 | +      },  | 
 | 80 | +      onRequest: async (request, reply) => {  | 
 | 81 | +        await fastify.authorize(request, reply, [  | 
 | 82 | +          AppRoles.LINKS_MANAGER,  | 
 | 83 | +          AppRoles.LINKS_ADMIN,  | 
 | 84 | +        ]);  | 
 | 85 | +      },  | 
 | 86 | +    },  | 
 | 87 | +    async (request, reply) => {  | 
 | 88 | +      // make sure that a user can manage this link, either via owning or being in a group that has access to it, or is a LINKS_ADMIN.  | 
 | 89 | +      throw new NotImplementedError({});  | 
 | 90 | +    },  | 
 | 91 | +  );  | 
 | 92 | +  fastify.get<NoDataRequest>(  | 
 | 93 | +    "/redir",  | 
 | 94 | +    {  | 
 | 95 | +      onRequest: async (request, reply) => {  | 
 | 96 | +        await fastify.authorize(request, reply, [  | 
 | 97 | +          AppRoles.LINKS_MANAGER,  | 
 | 98 | +          AppRoles.LINKS_ADMIN,  | 
 | 99 | +        ]);  | 
 | 100 | +      },  | 
 | 101 | +    },  | 
 | 102 | +    async (request, reply) => {  | 
 | 103 | +      // if an admin, show all links  | 
 | 104 | +      // if a links manager, show all my links + links I can manage  | 
 | 105 | +      throw new NotImplementedError({});  | 
 | 106 | +    },  | 
 | 107 | +  );  | 
 | 108 | +};  | 
 | 109 | + | 
 | 110 | +export default linkryRoutes;  | 
0 commit comments