Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/superdeno/test/deps.ts>Opine.Opine

Super-agent driven library for testing Deno HTTP servers.
Latest
interface Opine.Opine
implements Application
Re-export
import { type Opine } from "https://deno.land/x/superdeno@4.9.0/test/deps.ts";
const { Opine } = Opine;

Call Signatures

(err?: any): void
(deferToNext: "router"): void

"Break-out" of a router by calling {next('router')};

Index Signatures

[key: string]: string

Type Parameters

optional
P extends Params = ParamsDictionary
optional
ResBody = any
optional
ReqQuery = any

Type Parameters

T
optional
Method extends
| "all"
| "get"
| "post"
| "put"
| "delete"
| "patch"
| "options"
| "head"
= any

Call Signatures

<P extends Params = ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = any>(path: PathParams, ...handlers: Array<RequestHandler<P, ResBody, ReqQuery>>): T
<P extends Params = ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = any>(path: PathParams, ...handlers: Array<RequestHandlerParams<P, ResBody>>): T
(path: PathParams, subApplication: Application): T

Properties

all: IRouterMatcher<this, "all">

Special-cased "all" method, applying the given route path, middleware, and callback to every HTTP method.

get: IRouterMatcher<this, "get">
post: IRouterMatcher<this, "post">
put: IRouterMatcher<this, "put">
delete: IRouterMatcher<this, "delete">
patch: IRouterMatcher<this, "patch">
options: IRouterMatcher<this, "options">
head: IRouterMatcher<this, "head">
checkout: IRouterMatcher<this>
connect: IRouterMatcher<this>
copy: IRouterMatcher<this>
lock: IRouterMatcher<this>
merge: IRouterMatcher<this>
mkactivity: IRouterMatcher<this>
mkcol: IRouterMatcher<this>
move: IRouterMatcher<this>
notify: IRouterMatcher<this>
propfind: IRouterMatcher<this>
proppatch: IRouterMatcher<this>
purge: IRouterMatcher<this>
report: IRouterMatcher<this>
subscribe: IRouterMatcher<this>
trace: IRouterMatcher<this>
unlock: IRouterMatcher<this>
unsubscribe: IRouterMatcher<this>
use: IRouterHandler<this> & IRouterMatcher<this>
params: any
_params: any[]
caseSensitive: boolean
mergeParams: boolean
strict: boolean
stack: any[]

Stack of configured routes

Methods

route(prefix: PathParams): IRoute
handle(): void

Dispatch a req, res pair into the application. Starts pipeline processing.

If no callback is provided, then default error handlers will respond in the event of an error bubbling through the stack.

param(name: string, fn: RequestParamHandler): void
process_params(
layer: any,
called: any,
): void

Properties

path: string
stack: any
all: IRouterHandler<this>
get: IRouterHandler<this>
post: IRouterHandler<this>
put: IRouterHandler<this>
delete: IRouterHandler<this>
patch: IRouterHandler<this>
options: IRouterHandler<this>
head: IRouterHandler<this>
checkout: IRouterHandler<this>
copy: IRouterHandler<this>
lock: IRouterHandler<this>
merge: IRouterHandler<this>
mkactivity: IRouterHandler<this>
mkcol: IRouterHandler<this>
move: IRouterHandler<this>
notify: IRouterHandler<this>
purge: IRouterHandler<this>
report: IRouterHandler<this>
subscribe: IRouterHandler<this>
trace: IRouterHandler<this>
unlock: IRouterHandler<this>
unsubscribe: IRouterHandler<this>
dispatch: RequestHandler

Properties

optional
caseSensitive: boolean
optional
mergeParams: boolean
optional
strict: boolean

Properties

start: number
end: number

Properties

start: number
end: number

Properties

type: string

Properties

optional
combine: boolean

The "combine" option can be set to true and overlapping & adjacent ranges will be combined into a single range.

Examples

app.get('/user/:id', (req, res) => res.send(req.params.id)); // implicitly ParamsDictionary app.get(/user/(.)/, (req, res) => res.send(req.params[0])); app.get('/user/', (req, res) => res.send(req.params[0]));

Type Parameters

optional
P extends Params = ParamsDictionary
optional
ResBody = any
optional
ReqQuery = any

Properties

get: (name: string) => string | undefined

Return request header.

The Referrer header field is special-cased, both Referrer and Referer are interchangeable.

Examples:

req.get('Content-Type');
// => "text/plain"

req.get('content-type');
// => "text/plain"

req.get('Something');
// => undefined
param: (name: string, defaultValue?: string) => string | undefined
protocol: string

Return the protocol string "http" or "https" when requested with TLS. When the "trust proxy" setting is enabled the "X-Forwarded-Proto" header field will be trusted. If you're running behind a reverse proxy that supplies https for you this may be enabled.

secure: boolean

Short-hand for:

req.protocol == 'https'

ip: string

Return the remote address, or when "trust proxy" is true return the upstream addr.

ips: string[]

When "trust proxy" is true, parse the "X-Forwarded-For" ip address list.

For example if the value were "client, proxy1, proxy2" you would receive the array ["client", "proxy1", "proxy2"] where "proxy2" is the furthest down-stream.

subdomains: string[]

Return subdomains as an array.

Subdomains are the dot-separated parts of the host before the main domain of the app. By default, the domain of the app is assumed to be the last two parts of the host. This can be changed by setting "subdomain offset".

For example, if the domain is "deno.dinosaurs.example.com": If "subdomain offset" is not set, req.subdomains is ["dinosaurs", "deno"]. If "subdomain offset" is 3, req.subdomains is ["deno"].

path: string

Returns the pathname of the URL.

optional
hostname: string

Parse the "Host" header field hostname.

fresh: boolean

Check if the request is fresh, aka Last-Modified and/or the ETag still match.

stale: boolean

Check if the request is stale, aka "Last-Modified" and / or the "ETag" for the resource has changed.

xhr: boolean

Check if the request was an XMLHttpRequest.

body: any

Body of request.

If the body has been passed such that a parsedBody property is defined, this returns the parsedBody value.

To always get the raw body value, use the raw property.

Raw body of request.

method: string
params: P
query: ReqQuery
route: any
originalUrl: string
url: string
baseUrl: string
proto: string
headers: Headers
conn: ConnInfo
optional
res: OpineResponse<ResBody>

After middleware.init executed, OpineRequest will contain res and next properties. See: opine/src/middleware/init.ts

optional
next: NextFunction
optional
_parsedBody: boolean

After body parsers, OpineRequest will contain _parsedBody boolean property dictating that the body has been parsed. See: opine/src/middleware/bodyParser/

optional
parsedBody: any

After body parsers, OpineRequest will contain parsedBody property containing the parsed body. See: opine/src/middleware/bodyParser/

optional
_parsedUrl: ParsedURL

After calling parseUrl on the request object, the parsed url is memoization by storing onto the _parsedUrl property.

optional
_parsedOriginalUrl: ParsedURL

After calling originalUrl on the request object, the original url is memoization by storing onto the _parsedOriginalUrl property.

finalResponse: Promise<Response>

Returns a promise that resolves to the response to the request.

Methods

accepts(): string | string[] | false

Check if the given type(s) is acceptable, returning the best match when true, otherwise undefined, in which case you should respond with 406 "Not Acceptable".

The type value may be a single mime type string such as "application/json", the extension name such as "json", a comma-delimited list such as "json, html, text/plain", or an array ["json", "html", "text/plain"]. When a list or array is given the best match, if any is returned.

Examples:

// Accept: text/html
req.accepts('html');
// => "html"

// Accept: text/*, application/json
req.accepts('html');
// => "html"
req.accepts('text/html');
// => "text/html"
req.accepts('json, text');
// => "json"
req.accepts('application/json');
// => "application/json"

// Accept: text/*, application/json
req.accepts('image/png');
req.accepts('png');
// => undefined

// Accept: text/*;q=.5, application/json
req.accepts(['html', 'json']);
req.accepts('html, json');
// => "json"
accepts(type: string): string | string[] | false
accepts(type: string[]): string | string[] | false
accepts(...type: string[]): string | string[] | false
acceptsCharsets(): string | string[] | false

Returns the first accepted charset of the specified character sets, based on the request's Accept-Charset HTTP header field. If none of the specified charsets is accepted, returns false.

For more information, or if you have issues or concerns, see accepts.

acceptsCharsets(charset: string): string | string[] | false
acceptsCharsets(charset: string[]): string | string[] | false
acceptsCharsets(...charset: string[]): string | string[] | false
acceptsEncodings(): string | string[] | false

Returns the first accepted encoding of the specified encodings, based on the request's Accept-Encoding HTTP header field. If none of the specified encodings is accepted, returns false.

For more information, or if you have issues or concerns, see accepts.

acceptsEncodings(encoding: string): string | string[] | false
acceptsEncodings(encoding: string[]): string | string[] | false
acceptsEncodings(...encoding: string[]): string | string[] | false
acceptsLanguages(): string | string[] | false

Returns the first accepted language of the specified languages, based on the request's Accept-Language HTTP header field. If none of the specified languages is accepted, returns false.

For more information, or if you have issues or concerns, see accepts.

acceptsLanguages(lang: string): string | string[] | false
acceptsLanguages(lang: string[]): string | string[] | false
acceptsLanguages(...lang: string[]): string | string[] | false
range(size: number, options?: RangeParserOptions): RangeParserRanges | RangeParserResult | undefined

Parse Range header field, capping to the given size.

Unspecified ranges such as "0-" require knowledge of your resource length. In the case of a byte range this is of course the total number of bytes. If the Range header field is not given undefined is returned. If the Range header field is given, return value is a result of range-parser. See more ./types/range-parser/index.d.ts

NOTE: remember that ranges are inclusive, so for example "Range: users=0-3" should respond with 4 users when available, not 3.

is(type: string | string[]): string | boolean | null

Check if the incoming request contains the "Content-Type" header field, and it contains the give mime type.

Examples:

 // With Content-Type: text/html; charset=utf-8
 req.is('html');
 req.is('text/html');
 req.is('text/*');
 // => true

 // When Content-Type is application/json
 req.is('json');
 req.is('application/json');
 req.is('application/*');
 // => true

 req.is('html');
 // => false
upgrade(): WebSocket

Upgrade an HTTP connection to a WebSocket connection by calling Deno.upgradeWebSocket.

Example:

app.get("/ws", async (req, _res, next) => {
  if (req.headers.get("upgrade") === "websocket") {
    const socket = req.upgrade();
    handleSocket(socket);
  } else {
    next();
  }
});
respond(response: { status?: number; statusText?: string; headers?: Headers; body?:
| Uint8Array
| string
; trailers?: () => Promise<Headers> | Headers; }
): void

Properties

value: string
quality: number
type: string
subtype: string

Type Parameters

optional
ResBody = any

Properties

optional
status: number
optional
statusText: string
optional
headers: Headers
optional
body:
| Uint8Array
| string
optional
req: OpineRequest
locals: any
optional
statusMessage: any
written: boolean

Boolean signifying whether the request has already been responded to.

json: Send<ResBody, this>

Send JSON response.

Examples:

res.json(null);
res.json({ user: 'deno' });
res.setStatus(500).json('oh noes!');
res.setStatus(404).json(`I don't have that`);
jsonp: Send<ResBody, this>

Send JSON response with JSONP callback support.

Examples:

res.jsonp(null);
res.jsonp({ user: 'deno' });
res.setStatus(500).jsonp('oh noes!');
res.setStatus(404).jsonp(`I don't have that`);
send: Send<ResBody, this>

Send a response.

Examples:

res.send(new Buffer('wahoo'));
res.send({ some: 'json' });
res.send('<p>some html</p>');
res.setStatus(404).send('Sorry, cant find that');

Methods

addResource(rid: number): void

Add a resource ID to the list of resources to be closed after the .end() method has been called.

append(field: string, value: string | string[]): this

Appends the specified value to the HTTP response header field. If the header is not already set, it creates the header with the specified value. The value parameter can be a string or an array.

Example:

res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly'); res.append('Warning', '199 Miscellaneous warning'); res.append("cache-control", ["public", "max-age=604800", "immutable"]);

Note: calling res.set() after res.append() will reset the previously-set header value.

attachment(filename?: string): this

Set Content-Disposition header to attachment with optional filename.

clearCookie(name: string, options?: CookieOptions): this

Clear cookie by name.

clearCookie(cookie: CookieWithOptionalValue): this

Clear the provided cookie.

download(path: string): Promise<this | void>

Transfer the file at the given path as an attachment.

Optionally providing an alternate attachment filename.

Optionally providing an options object to use with res.sendFile().

This function will set the Content-Disposition header, overriding any existing Content-Disposition header in order to set the attachment and filename.

This method uses res.sendFile().

download(path: string, filename: string): Promise<this | void>
download(
path: string,
filename: string,
options: any,
): Promise<this | void>
etag(chunk: string | Uint8Array | Deno.FileInfo): this

Sets an ETag header.

end(): Promise<void>

Ends a response.

Generally it is recommended to use the res.send() or res.json() methods which will handle automatically setting the Content-Type header, and are able to gracefully handle a greater range of body inputs.

Examples:

res.end();
res.end('<p>some html</p>');
res.setStatus(404).end('Sorry, cant find that');
end(body: DenoResponseBody): Promise<void>
format(obj: any): this

Respond to the Acceptable formats using an obj of mime-type callbacks.

This method uses req.accepted, an array of acceptable types ordered by their quality values. When "Accept" is not present the first callback is invoked, otherwise the first match is used. When no match is performed the server responds with 406 "Not Acceptable".

Content-Type is set for you, however if you choose you may alter this within the callback using res.type() or res.set('Content-Type', ...).

res.format({ 'text/plain': function(){ res.send('hey'); },

 'text/html': function(){
   res.send('<p>hey</p>');
 },

 'application/json': function(){
   res.send({ message: 'hey' });
 }

});

In addition to canonicalized MIME types you may also use extnames mapped to these types:

res.format({ text: function(){ res.send('hey'); },

 html: function(){
   res.send('<p>hey</p>');
 },

 json: function(){
   res.send({ message: 'hey' });
 }

});

By default Express passes an Error with a .status of 406 to next(err) if a match is not made. If you provide a .default callback it will be invoked instead.

get(field: string): string

Get value for header field.

location(url: string): this

Set the location header to url.

The given url can also be the name of a mapped url, for example by default express supports "back" which redirects to the Referrer or Referer headers or "/".

Examples:

res.location('/foo/bar').; res.location('http://example.com'); res.location('../login'); // /blog/post/1 -> /blog/login

Mounting:

When an application is mounted and res.location() is given a path that does not lead with "/" it becomes relative to the mount-point. For example if the application is mounted at "/blog", the following would become "/blog/login".

 res.location('login');

While the leading slash would result in a location of "/login":

 res.location('/login');
redirect(url: string): void

Redirect to the given url with optional response status defaulting to 302.

The resulting url is determined by res.location().

Examples:

res.redirect('/foo/bar'); res.redirect('http://example.com'); res.redirect(301, 'http://example.com'); res.redirect('../login'); // /blog/post/1 -> /blog/login

redirect(code: Status, url: string): void
removeHeader(field: string): this

Remove a header from the response

Examples:

res.removeHeader('Accept');
render(
view: string,
options?: Record<string, unknown>,
callback?: (err: Error, html: string) => void,
): void

Render view with the given options and optional callback fn. When a callback function is given a response will not be made automatically, otherwise a response of 200 and text/html is given.

Options:

  • cache boolean hinting to the engine it should cache
  • filename filename of the view being rendered
render(view: string, callback?: (err: Error, html: string) => void): void
sendFile(path: string, options?: any): Promise<this | void>

Transfer the file at the given path.

Automatically sets the Content-Type response header field.

Examples:

The following example illustrates how res.sendFile() may be used as an alternative for the static() middleware for dynamic situations. The code backing res.sendFile() is actually the same code, so HTTP cache support etc is identical.

app.get('/user/:uid/photos/:file', function(req, res){
  const uid = req.params.uid;
  const file = req.params.file;

  req.user.mayViewFilesFrom(uid, function(yes) {
    if (yes) {
      res.sendFile('/uploads/' + uid + '/' + file);
    } else {
      res.send(403, 'Sorry! you cant see that.');
    }
  });
});
sendStatus(code: Status): this

Set the response HTTP status code to statusCode and send its string representation as the response body.

Examples:

res.sendStatus(200); // equivalent to res.setStatus(200).send('OK') res.sendStatus(403); // equivalent to res.setStatus(403).send('Forbidden') res.sendStatus(404); // equivalent to res.setStatus(404).send('Not Found') res.sendStatus(500); // equivalent to res.setStatus(500).send('Internal Server Error')

set(field: string, value: string): this

Set header field to val, or pass an object of header fields.

Examples:

res.set('Accept', 'application/json');
res.set({
  'Accept-Language': en-US, en;q=0.5,
  'Accept': 'text/html',
});
set(obj: Record<string, string>): this
setHeader(field: string, value: string): this

Set header field to value, or pass an object of header fields.

alias for res.set()

Examples:

res.setHeader('Accept', 'application/json');
res.setHeader({
  'Accept-Language': "en-US, en;q=0.5",
  'Accept': 'text/html',
});
setHeader(obj: Record<string, string>): this
setStatus(code: Status): this

Set status code.

type(type: string): this

Set Content-Type response header with type.

Examples:

res.type('.html');
res.type('html');
res.type('json');
res.type('application/json');
res.type('png');
unset(field: string): this

Removes the header field.

Examples:

res.unset('Accept');

vary(field: string): this

Adds the field to the Vary response header, if it is not there already. Examples:

res.vary('User-Agent').render('docs');

Call Signatures

(req: OpineRequest, res?: OpineResponse): void

Opine instance itself is a request handler, which could be invoked without third argument.

Properties

get: ((setting: string) => any) & IRouterMatcher<this>
router: string
settings: any
locals: any
routes: any

The app.routes object houses all of the routes defined mapped by the associated HTTP verb. This object may be used for introspection capabilities, for example Opine uses this internally not only for routing but to provide default OPTIONS behaviour unless app.options() is used. Your application or framework may also remove routes by simply by removing them from this object.

_router: Router

Used to get all registered routes in Opine Application

mountpath: string | string[]

The app.mountpath property contains one or more path patterns on which a sub-app was mounted.

cache: any
parent: any
engines: any

Methods

init(): void

Initialize the server.

  • setup default configuration
  • setup default middleware
  • setup route reflection methods
defaultConfiguration(): void

Initialize application configuration.

lazyrouter(): void

Lazily adds the base router if it has not yet been added.

We cannot add the base router in the defaultConfiguration because it reads app settings which might be set after that has run.

set(setting: string, value?: any): this

Assign setting to val, or return setting's value.

app.set('foo', 'bar'); app.get('foo'); // => "bar"

Mounted servers inherit their parent server's settings.

param(name: string | string[], fn: RequestParamHandler): this
path(): string

Return the app's absolute pathname based on the parent(s) that have mounted it.

For example if the application was mounted as "/admin", which itself was mounted as "/blog" then the return value would be "/blog/admin".

enabled(setting: string): boolean

Check if setting is enabled (truthy).

app.enabled('foo') // => false

app.enable('foo') app.enabled('foo') // => true

disabled(setting: string): boolean

Check if setting is disabled.

app.disabled('foo') // => true

app.enable('foo') app.disabled('foo') // => false

enable(setting: string): this

Enable setting.

disable(setting: string): this

Disable setting.

listen(): Server

Listen for connections.

A Deno Server is returned.

listen(port: number, callback?: () => void): Server
listen(addr: string, callback?: () => void): Server
listen(options: HTTPOptions, callback?: () => void): Server
listen(options: HTTPSOptions, callback?: () => void): Server
on(event: string, callback: (args: any) => any): any

The mount event is fired on a sub-app, when it is mounted on a parent app. The parent app is passed to the callback function.

NOTE: Sub-apps will:

  • Not inherit the value of settings that have a default value. You must set the value in the sub-app.
  • Inherit the value of settings with no default value.
emit(event: string, arg: any): any

Emit an event using the applications event emitter.

Events will be raised based on the passed event string and any listening on() methods will receive the passed arg as an argument.

engine(ext: string, fn: (
path: string,
options: Record<string, unknown>,
callback: (e: any, rendered: string) => void,
) => void
): this

Register the given template engine callback fn for the provided extension ext.

render(
name: string,
options?: any,
callback?: (err: Error, html: string) => void,
): void

Render the given view name name with options and a callback accepting an error and the rendered template string.

Example:

app.render('email', { name: 'Deno' }, function(err, html) { // ... })

render(name: string, callback: (err: Error, html: string) => void): void
type alias Opine.Opine
Re-export
import { type Opine } from "https://deno.land/x/superdeno@4.9.0/test/deps.ts";
const { Opine } = Opine;
definition: Omit<Deno.ListenOptions, "transport">
definition: Omit<Deno.ListenTlsOptions & { certFile: string; keyFile: string; }, "transport">
definition:
| undefined
| string
| Uint8Array
definition:
| null
| undefined
| number
| boolean
| Record<string, unknown>
definition: string[]

Type Parameters

optional
P extends Params = ParamsDictionary
optional
ResBody = any
optional
ReqQuery = any
definition: () => any
definition: string | RegExp | Array<string | RegExp>

Type Parameters

optional
P extends Params = ParamsDictionary
optional
ResBody = any
optional
ReqQuery = any
definition: Omit<Cookie, "value"> & { value?: Cookie["value"]; }
definition: Omit<Cookie, "name" | "value">
definition: (err: Error) => void
definition: URL & { path?: string | null; query?: string | null; _raw?: string | null; }
definition: -1
definition: -2

Type Parameters

optional
ResBody = any
optional
T = OpineResponse<ResBody>
definition: (body?: ResBody) => T
definition: (
value: any,
name: string,
) => any
definition: IRouterHandler<T> & IRouterMatcher<T> & ((...handlers: RequestHandlerParams[]) => T)
variable Opine.Opine
Re-export
import { Opine } from "https://deno.land/x/superdeno@4.9.0/test/deps.ts";
const { Opine } = Opine;

Supported Deno methods.

function Opine.Opine
Re-export
import { Opine } from "https://deno.land/x/superdeno@4.9.0/test/deps.ts";
const { Opine } = Opine;

Create a middleware to parse JSON bodies.

Parameters

optional
options: any = [UNSUPPORTED]