MCPcopy
hub / github.com/tinyhttp/tinyhttp / use

Method use

packages/app/src/app.ts:174–222  ·  view source on GitHub ↗
(...args: UseMethodParams<Req, Res, App>)

Source from the content-addressed store, hash-verified

172 return this
173 }
174 use(...args: UseMethodParams<Req, Res, App>) {
175 const base = args[0]
176
177 const fns = args.slice(1).flat()
178
179 if (base instanceof App) {
180 // Set App parent to current App
181 base.parent = this
182
183 // Mount on root
184 base.mountpath = '/'
185
186 this.apps['/'] = base
187 }
188
189 const path = typeof base === 'string' ? base : '/'
190
191 let regex: { keys: string[]; pattern: RegExp }
192
193 for (const fn of fns) {
194 if (fn instanceof App) {
195 regex = rg(path, true)
196
197 fn.mountpath = path
198
199 this.apps[path] = fn
200
201 fn.parent = this
202 }
203 }
204
205 if (base === '/') {
206 for (const fn of fns) super.use(base, mount(fn as Handler))
207 } else if (typeof base === 'function' || base instanceof App) {
208 super.use('/', [base, ...fns].map(mount))
209 } else if (Array.isArray(base)) {
210 super.use('/', [...base, ...fns].map(mount))
211 } else {
212 pushMiddleware(this.middleware)({
213 path: base as string,
214 regex,
215 type: 'mw',
216 handler: mount(fns[0] as Handler),
217 handlers: fns.slice(1).map(mount)
218 })
219 }
220
221 return this // chainable
222 }
223 /**
224 * Register a template engine with extension
225 */

Callers 15

routeMethod · 0.95
InitAppAndTestFunction · 0.95
createColorTestFunction · 0.95
createEmojiTestFunction · 0.95
createAppWithFunction · 0.95
index.jsFile · 0.45
index.jsFile · 0.45
server.jsFile · 0.45
index.jsFile · 0.45
index.jsFile · 0.45
index.jsFile · 0.45
index.tsFile · 0.45

Calls 2

pushMiddlewareFunction · 0.90
mountFunction · 0.85

Tested by 3

createColorTestFunction · 0.76
createEmojiTestFunction · 0.76
createAppWithFunction · 0.76