buddy

node MVC discord bot
Log | Files | Refs | README

APIRouter.js (1657B)


      1 'use strict';
      2 
      3 const noop = () => {}; // eslint-disable-line no-empty-function
      4 const methods = ['get', 'post', 'delete', 'patch', 'put'];
      5 const reflectors = [
      6   'toString',
      7   'valueOf',
      8   'inspect',
      9   'constructor',
     10   Symbol.toPrimitive,
     11   Symbol.for('nodejs.util.inspect.custom'),
     12 ];
     13 
     14 function buildRoute(manager) {
     15   const route = [''];
     16   const handler = {
     17     get(target, name) {
     18       if (reflectors.includes(name)) return () => route.join('/');
     19       if (methods.includes(name)) {
     20         const routeBucket = [];
     21         for (let i = 0; i < route.length; i++) {
     22           // Reactions routes and sub-routes all share the same bucket
     23           if (route[i - 1] === 'reactions') break;
     24           // Literal IDs should only be taken account if they are the Major ID (the Channel/Guild ID)
     25           if (/\d{16,19}/g.test(route[i]) && !/channels|guilds/.test(route[i - 1])) routeBucket.push(':id');
     26           // All other parts of the route should be considered as part of the bucket identifier
     27           else routeBucket.push(route[i]);
     28         }
     29         return options =>
     30           manager.request(
     31             name,
     32             route.join('/'),
     33             Object.assign(
     34               {
     35                 versioned: manager.versioned,
     36                 route: routeBucket.join('/'),
     37               },
     38               options,
     39             ),
     40           );
     41       }
     42       route.push(name);
     43       return new Proxy(noop, handler);
     44     },
     45     apply(target, _, args) {
     46       route.push(...args.filter(x => x != null)); // eslint-disable-line eqeqeq
     47       return new Proxy(noop, handler);
     48     },
     49   };
     50   return new Proxy(noop, handler);
     51 }
     52 
     53 module.exports = buildRoute;