import{r as a,p as r,q as o,w as i,z as e,R as s}from"./vendor.3d5b4206.js";const c=e("div",{class:"markdown-body"},[e("blockquote",null,[e("p",null,"Custom API Endpoints register new API routes which can be used to infinitely extend the core functionality of the platform.")]),e("h2",{id:"extension-entrypoint"},[s("Extension Entrypoint "),e("a",{class:"header-anchor",href:"#extension-entrypoint"},"#")]),e("p",null,[s("The entrypoint of your endpoint is the "),e("code",null,"index"),s(" file inside the "),e("code",null,"src/"),s(" folder of your extension package. It exports a register function to register one or more custom routes. Each route of your endpoint will be a sub-route of "),e("code",null,"/<extension-name>"),s(".")]),e("div",{class:"tip hint"},[e("div",{class:"hint-title"},"Extension Name"),e("p",null,"The extension name is usually the name of the folder where you put your extension when deploying it.")]),e("p",null,"Example of an entrypoint:"),e("pre",null,[e("code",{class:"language-js"},[e("span",{class:"hljs-keyword"},"export"),s(),e("span",{class:"hljs-keyword"},"default"),s(` (router) => {
	router.get(`),e("span",{class:"hljs-string"},"'/'"),s(", "),e("span",{class:"hljs-function"},[s("("),e("span",{class:"hljs-params"},"req, res"),s(") =>")]),s(" res.send("),e("span",{class:"hljs-string"},"'Hello, World!'"),s(`));
};
`)])]),e("p",null,"Alternatively, you can export a configuration object to be able to customize the root route:"),e("pre",null,[e("code",{class:"language-js"},[e("span",{class:"hljs-keyword"},"export"),s(),e("span",{class:"hljs-keyword"},"default"),s(` {
	`),e("span",{class:"hljs-attr"},"id"),s(": "),e("span",{class:"hljs-string"},"'greet'"),s(`,
	`),e("span",{class:"hljs-attr"},"register"),s(": "),e("span",{class:"hljs-function"},[s("("),e("span",{class:"hljs-params"},"router"),s(") =>")]),s(` {
		router.get(`),e("span",{class:"hljs-string"},"'/'"),s(", "),e("span",{class:"hljs-function"},[s("("),e("span",{class:"hljs-params"},"req, res"),s(") =>")]),s(" res.send("),e("span",{class:"hljs-string"},"'Hello, World!'"),s(`));
		router.get(`),e("span",{class:"hljs-string"},"'/intro'"),s(", "),e("span",{class:"hljs-function"},[s("("),e("span",{class:"hljs-params"},"req, res"),s(") =>")]),s(" res.send("),e("span",{class:"hljs-string"},"'Nice to meet you.'"),s(`));
		router.get(`),e("span",{class:"hljs-string"},"'/goodbye'"),s(", "),e("span",{class:"hljs-function"},[s("("),e("span",{class:"hljs-params"},"req, res"),s(") =>")]),s(" res.send("),e("span",{class:"hljs-string"},"'Goodbye!'"),s(`));
	},
};
`)])]),e("p",null,[s("The routes of this endpoint are accessible at "),e("code",null,"/greet"),s(", "),e("code",null,"/greet/intro"),s(" and "),e("code",null,"/greet/goodbye"),s(".")]),e("h4",{id:"available-options"},[s("Available Options "),e("a",{class:"header-anchor",href:"#available-options"},"#")]),e("ul",null,[e("li",null,[e("code",null,"id"),s(" \u2014 The unique key for this endpoint. Each route of your endpoint will be a sub-route of "),e("code",null,"/<id>"),s(".")]),e("li",null,[e("code",null,"register"),s(" \u2014 The endpoint\u2019s register function.")])]),e("h2",{id:"register-function"},[s("Register Function "),e("a",{class:"header-anchor",href:"#register-function"},"#")]),e("p",null,[s("The register function receives the two parameters "),e("code",null,"router"),s(" and "),e("code",null,"context"),s(". "),e("code",null,"router"),s(" is an Express router instance. "),e("code",null,"context"),s(" is an object with the following properties:")]),e("ul",null,[e("li",null,[e("code",null,"services"),s(" \u2014 All API internal services.")]),e("li",null,[e("code",null,"exceptions"),s(" \u2014\xA0API exception objects that can be used to throw \u201Cproper\u201D errors.")]),e("li",null,[e("code",null,"database"),s(" \u2014 Knex instance that is connected to the current database.")]),e("li",null,[e("code",null,"getSchema"),s(" \u2014 Async function that reads the full available schema for use in services")]),e("li",null,[e("code",null,"env"),s(" \u2014\xA0Parsed environment variables.")]),e("li",null,[e("code",null,"logger"),s(" \u2014\xA0"),e("a",{href:"https://github.com/pinojs/pino",target:"_blank",rel:"noopener noreferrer"},"Pino"),s(" instance.")]),e("li",null,[e("code",null,"emitter"),s(" \u2014 "),e("a",{href:"https://github.com/directus/directus/blob/main/api/src/emitter.ts",target:"_blank",rel:"noopener noreferrer"},"Event emitter"),s(" instance that can be used to trigger custom events for other extensions.")])]),e("div",{class:"warning hint"},[e("div",{class:"hint-title"},"Event loop"),e("p",null,"When implementing custom events using the emitter make sure you never directly or indirectly emit the same event your hook is currently handling as that would result in an infinite loop!")]),e("h2",{id:"example%3A-recipes"},[s("Example: Recipes "),e("a",{class:"header-anchor",href:"#example%3A-recipes"},"#")]),e("pre",null,[e("code",{class:"language-js"},[e("span",{class:"hljs-keyword"},"export"),s(),e("span",{class:"hljs-keyword"},"default"),s(` (router, { services, exceptions }) => {
	`),e("span",{class:"hljs-keyword"},"const"),s(` { ItemsService } = services;
	`),e("span",{class:"hljs-keyword"},"const"),s(` { ServiceUnavailableException } = exceptions;

	router.get(`),e("span",{class:"hljs-string"},"'/'"),s(", "),e("span",{class:"hljs-function"},[s("("),e("span",{class:"hljs-params"},"req, res, next"),s(") =>")]),s(` {
		`),e("span",{class:"hljs-keyword"},"const"),s(" recipeService = "),e("span",{class:"hljs-keyword"},"new"),s(" ItemsService("),e("span",{class:"hljs-string"},"'recipes'"),s(", { "),e("span",{class:"hljs-attr"},"schema"),s(": req.schema, "),e("span",{class:"hljs-attr"},"accountability"),s(`: req.accountability });

		recipeService
			.readByQuery({ `),e("span",{class:"hljs-attr"},"sort"),s(": [{ "),e("span",{class:"hljs-attr"},"column"),s(": "),e("span",{class:"hljs-string"},"'name'"),s(", "),e("span",{class:"hljs-attr"},"order"),s(": "),e("span",{class:"hljs-string"},"'asc'"),s(" }], "),e("span",{class:"hljs-attr"},"fields"),s(": ["),e("span",{class:"hljs-string"},"'*'"),s(`] })
			.then(`),e("span",{class:"hljs-function"},[s("("),e("span",{class:"hljs-params"},"results"),s(") =>")]),s(` res.json(results))
			.catch(`),e("span",{class:"hljs-function"},[s("("),e("span",{class:"hljs-params"},"error"),s(") =>")]),s(` {
				`),e("span",{class:"hljs-keyword"},"return"),s(" next("),e("span",{class:"hljs-keyword"},"new"),s(` ServiceUnavailableException(error.message));
			});
	});
};
`)])])],-1),g={setup(p,{expose:t}){const n={title:"Custom API Endpoints",modularExtension:!0};return t({frontmatter:n}),(u,h)=>{const l=a("DocsWrapper");return r(),o(l,{frontmatter:n},{default:i(()=>[c]),_:1})}}};export{g as default};
