Last active
May 5, 2024 20:28
-
-
Save dansp89/dde3840fe0b2043e062391f58a001d9e to your computer and use it in GitHub Desktop.
STRAPI 4 - Add custom routes and also add to the SWAGGER documentation.
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
> With this code, you can add your custom routes in swagger, read the documentation if you have any further questions. | |
## Tested on: | |
1. Node 18.17.1 | |
1. Strapi 4.15.5 | |
1. Language: Typescript |
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 ./src/api/<content-type>/routes/01-custom.ts | |
*/ | |
export default { | |
routes: [ | |
{ | |
method: 'GET', | |
path: '/users-x/find/email/:email([\\w@.-]+)', | |
handler: 'user-x.findByEmail', | |
}, | |
{ | |
method: 'GET', | |
path: '/users-x/find/username/:username([a-z]+)', | |
handler: 'user-x.findByUsername', | |
config: { | |
auth: false | |
} | |
} | |
] | |
} |
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 ./config/helpers.ts | |
* @author DanSP <https://t.me/dansp89> | |
*/ | |
/** | |
* Creates a custom router by combining internal routes with extra routes. | |
* @param {object} innerRouter - The internal router to be combined. | |
* @param {object[]} [extraRoutes=[]] - Extra routes to be added to the router. | |
* @returns {object} - Returns an object representing the custom router. | |
*/ | |
export const customRouter = (innerRouter, extraRoutes = []) => { | |
let routes; | |
let rotas = { | |
/** | |
* Gets the prefix of the internal router. | |
* @returns {string} - The router's prefix. | |
*/ | |
get prefix() { return innerRouter.prefix }, | |
/** | |
* Gets the combined routes from the internal router and extra routes. | |
* @returns {object[]} - An array of objects representing the combined routes. | |
*/ | |
get routes() { | |
if (!routes) routes = innerRouter.routes.concat(extraRoutes); | |
// let routerClean = removeDuplicateEndpoints(routes); | |
let routerClean = routes; | |
return routerClean; | |
} | |
}; | |
console.log("ROTAS", rotas); | |
return rotas; | |
}; |
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
export default { | |
/** | |
* @file ./src/index.ts | |
* An asynchronous register function that runs before | |
* your application is initialized. | |
* | |
* This gives you an opportunity to extend code. | |
* @source <https://docs.strapi.io/dev-docs/plugins/documentation#providing-replacement-documentation> | |
*/ | |
register({ strapi }) { | |
if (strapi.plugin('documentation')) { | |
const override = { | |
// Only run this override for version 1.0.0 | |
info: { version: '1.0.0' }, | |
paths: { | |
"/users-x/find/email/{email}": { | |
"get": { | |
"responses": { | |
"200": { | |
"description": "OK", | |
"content": { | |
"application/json": { | |
"schema": { | |
"$ref": "#/components/schemas/UserXResponse" | |
} | |
} | |
} | |
}, | |
"400": { | |
"description": "Bad Request", | |
"content": { | |
"application/json": { | |
"schema": { | |
"$ref": "#/components/schemas/Error" | |
} | |
} | |
} | |
}, | |
"401": { | |
"description": "Unauthorized", | |
"content": { | |
"application/json": { | |
"schema": { | |
"$ref": "#/components/schemas/Error" | |
} | |
} | |
} | |
}, | |
"403": { | |
"description": "Forbidden", | |
"content": { | |
"application/json": { | |
"schema": { | |
"$ref": "#/components/schemas/Error" | |
} | |
} | |
} | |
}, | |
"404": { | |
"description": "Not Found", | |
"content": { | |
"application/json": { | |
"schema": { | |
"$ref": "#/components/schemas/Error" | |
} | |
} | |
} | |
}, | |
"500": { | |
"description": "Internal Server Error", | |
"content": { | |
"application/json": { | |
"schema": { | |
"$ref": "#/components/schemas/Error" | |
} | |
} | |
} | |
} | |
}, | |
"tags": [ | |
"User-x" | |
], | |
"parameters": [ | |
{ | |
"name": "email", | |
"in": "path", | |
"description": "", | |
"deprecated": false, | |
"required": true, | |
"schema": { | |
"type": "string" | |
} | |
} | |
], | |
"operationId": "get/users-x/find/email/{email}" | |
} | |
}, | |
"/users-x/find/username/{username}": { | |
"get": { | |
"responses": { | |
"200": { | |
"description": "OK", | |
"content": { | |
"application/json": { | |
"schema": { | |
"$ref": "#/components/schemas/UserXResponse" | |
} | |
} | |
} | |
}, | |
"400": { | |
"description": "Bad Request", | |
"content": { | |
"application/json": { | |
"schema": { | |
"$ref": "#/components/schemas/Error" | |
} | |
} | |
} | |
}, | |
"401": { | |
"description": "Unauthorized", | |
"content": { | |
"application/json": { | |
"schema": { | |
"$ref": "#/components/schemas/Error" | |
} | |
} | |
} | |
}, | |
"403": { | |
"description": "Forbidden", | |
"content": { | |
"application/json": { | |
"schema": { | |
"$ref": "#/components/schemas/Error" | |
} | |
} | |
} | |
}, | |
"404": { | |
"description": "Not Found", | |
"content": { | |
"application/json": { | |
"schema": { | |
"$ref": "#/components/schemas/Error" | |
} | |
} | |
} | |
}, | |
"500": { | |
"description": "Internal Server Error", | |
"content": { | |
"application/json": { | |
"schema": { | |
"$ref": "#/components/schemas/Error" | |
} | |
} | |
} | |
} | |
}, | |
"tags": [ | |
"User-x" | |
], | |
"parameters": [ | |
{ | |
"name": "username", | |
"in": "path", | |
"description": "", | |
"deprecated": false, | |
"required": true, | |
"schema": { | |
"type": "string" | |
} | |
} | |
], | |
"operationId": "get/users-x/find/username/{username}" | |
} | |
}, | |
}, | |
} | |
strapi | |
.plugin('documentation') | |
.service('override') | |
.registerOverride(override, { }); | |
} | |
}, | |
/** | |
* An asynchronous bootstrap function that runs before | |
* your application gets started. | |
* | |
* This gives you an opportunity to set up your data model, | |
* run jobs, or perform some special logic. | |
*/ | |
bootstrap(/*{ strapi }*/) {}, | |
}; |
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 ./src/api/<content-type>/routes/<content-type>.ts | |
* user-x router | |
* @source Creating custom routers <https://docs.strapi.io/dev-docs/backend-customization/routes#creating-custom-routers> | |
* @source Custom core routes <https://docs.strapi.io/dev-docs/backend-customization/routes> | |
* @author DanSP <https://t.me/dansp89> | |
*/ | |
import { factories } from '@strapi/strapi'; | |
import newRouter from './01-custom'; | |
import { customRouter } from '../../../../config/helpers'; | |
// export default factories.createCoreRouter('api::user-x.user-x'); // Original router | |
export default customRouter( factories.createCoreRouter('api::user-x.user-x', { | |
only: [] // ['find', 'findOne', 'create', 'delete', 'update'] // Only those in the array will be displayed | |
}), newRouter?.routes ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment