Comparing version 0.2.2 to 0.2.3
@@ -65,5 +65,11 @@ "use strict"; | ||
} | ||
error(message) { | ||
console.error(`${color_js_1.FgRed}${message}${color_js_1.Reset}`); | ||
} | ||
infof(message, ...arg) { | ||
console.info(`${color_js_1.FgYellow}%s${color_js_1.Reset}`, util.format(message, ...arg)); | ||
} | ||
json(data) { | ||
console.log(JSON.stringify(data, null, 2)); | ||
} | ||
prompt(question) { | ||
@@ -70,0 +76,0 @@ return new Promise((resolve) => { |
@@ -47,3 +47,3 @@ "use strict"; | ||
createErrorWithIdentifier(id, message) { | ||
let idstr = 'unk'; | ||
let idstr = null; | ||
// @ts-ignore | ||
@@ -57,3 +57,3 @@ if (id.name) { | ||
} | ||
return new Error(message.replace('%id', idstr)); | ||
return new Error(message.replace('%id', idstr || 'unk')); | ||
} | ||
@@ -60,0 +60,0 @@ /** |
@@ -65,4 +65,4 @@ "use strict"; | ||
// put view provider in context | ||
ctx.set(consts_js_1.HTTP_KEY_REQ_ID, reqid); | ||
ctx.set(consts_js_1.HTTP_KEY_VIEW_PROVIDER, this.viewProvider); | ||
ctx.put(consts_js_1.HTTP_KEY_REQ_ID, reqid); | ||
ctx.put(consts_js_1.HTTP_KEY_VIEW_PROVIDER, this.viewProvider); | ||
// execute application middlewares | ||
@@ -80,2 +80,8 @@ await this.composedMiddleware(ctx, async (ctx, next) => { | ||
}); | ||
if (ctx.aborted && (this.viewProvider && ctx.accepts('html'))) { | ||
ctx.reply({ | ||
status: ctx.response.status, | ||
body: await this.viewProvider.renderError(ctx, { status: ctx.response.status, ...ctx.response.body }), | ||
}); | ||
} | ||
} | ||
@@ -89,6 +95,4 @@ catch (err) { | ||
const resultError = result_js_1.ResultError.try(err); | ||
if (ctx.accepts('html')) { | ||
if (this.viewProvider) { | ||
await this.viewProvider.renderError(ctx, resultError); | ||
} | ||
if (this.viewProvider && ctx.accepts('html')) { | ||
await this.viewProvider.renderError(ctx, resultError); | ||
} | ||
@@ -95,0 +99,0 @@ else { |
@@ -33,2 +33,4 @@ "use strict"; | ||
state = {}; | ||
response = { status: 404 }; | ||
aborted = false; | ||
_reply = null; | ||
@@ -89,12 +91,13 @@ constructor(ctx) { | ||
json(data, status = 200) { | ||
this.ctx.status = status; | ||
this.ctx.body = data; | ||
this.headers.set('content-type', 'application/json'); | ||
this.ctx.status = this.response.status = status; | ||
this.ctx.body = this.response.body = data; | ||
} | ||
noContent() { | ||
this.ctx.status = 204; | ||
this.ctx.body = undefined; | ||
this.ctx.status = this.response.status = 204; | ||
this.ctx.body = this.response.body = undefined; | ||
} | ||
created() { | ||
this.ctx.status = 201; | ||
this.ctx.body = undefined; | ||
this.ctx.status = this.response.status = 201; | ||
this.ctx.body = this.response.body = undefined; | ||
} | ||
@@ -105,21 +108,19 @@ throw(status, message) { | ||
abort(...args) { | ||
this.aborted = true; | ||
if (args.length == 1) { | ||
if (typeof args[0] == 'number') { | ||
this.ctx.status = args[0]; | ||
this.ctx.body = { message: 'Something went wrong.' }; | ||
this.ctx.status = this.response.status = args[0]; | ||
this.ctx.body = this.response.body = { message: 'Something went wrong.' }; | ||
return; | ||
} | ||
const err = args[0]; | ||
this.ctx.status = err.status; | ||
this.ctx.body = { message: err.message, ...(err.data || {}) }; | ||
this.ctx.status = this.response.status = err.status; | ||
this.ctx.body = this.response.body = { message: err.message, ...(err.data || {}) }; | ||
return; | ||
} | ||
this.ctx.status = args[0]; | ||
this.ctx.body = { message: args[1] || 'Something went wrong.', ...(args[2] || {}) }; | ||
this.ctx.status = this.response.status = args[0]; | ||
this.ctx.body = this.response.body = { message: args[1] || 'Something went wrong.', ...(args[2] || {}) }; | ||
} | ||
/** | ||
* ununsed | ||
* @param response | ||
*/ | ||
reply(response) { | ||
this.response = response; | ||
if (response.headers) { | ||
@@ -134,5 +135,5 @@ for (const key in response.headers) { | ||
stream(stream, mime = 'application/octet-stream') { | ||
this.ctx.status = 200; | ||
this.ctx.status = this.response.status = 200; | ||
this.ctx.type = mime; | ||
this.ctx.body = stream; | ||
this.ctx.body = this.response.body = stream; | ||
} | ||
@@ -147,3 +148,4 @@ redirect(url, alt) { | ||
} | ||
this.ctx.status = status; | ||
this.ctx.status = this.response.status = status; | ||
this.response.body = { template, data }; | ||
this.ctx.body = await view.render(this, template, data); | ||
@@ -158,5 +160,14 @@ this.willRender = true; | ||
// } | ||
/** | ||
* @deprecated use put() instead | ||
* @param key | ||
* @param value | ||
* @returns | ||
*/ | ||
set(key, value) { | ||
return this.state[key] = value; | ||
} | ||
put(key, value) { | ||
return this.state[key] = value; | ||
} | ||
value(key) { | ||
@@ -163,0 +174,0 @@ return this.state[key]; |
@@ -11,2 +11,5 @@ "use strict"; | ||
} | ||
put(key, value) { | ||
return this.state[key] = value; | ||
} | ||
value(key) { | ||
@@ -13,0 +16,0 @@ return this.state[key]; |
@@ -21,2 +21,3 @@ "use strict"; | ||
}; | ||
aborted = false; | ||
_reply = null; | ||
@@ -126,2 +127,3 @@ parseHeaders(headers) { | ||
abort(...args) { | ||
this.aborted = true; | ||
if (args.length == 1) { | ||
@@ -174,2 +176,5 @@ if (typeof args[0] == 'number') { | ||
} | ||
put(key, value) { | ||
return this.state[key] = value; | ||
} | ||
value(key) { | ||
@@ -176,0 +181,0 @@ return this.state[key]; |
import * as util from 'node:util'; | ||
import * as readline from 'node:readline'; | ||
import { FgYellow, Reset } from './color.js'; | ||
import { FgRed, FgYellow, Reset } from './color.js'; | ||
import { composeAsync } from '../support/compose.js'; | ||
@@ -62,5 +62,11 @@ import { AsyncLocalStorage } from 'node:async_hooks'; | ||
} | ||
error(message) { | ||
console.error(`${FgRed}${message}${Reset}`); | ||
} | ||
infof(message, ...arg) { | ||
console.info(`${FgYellow}%s${Reset}`, util.format(message, ...arg)); | ||
} | ||
json(data) { | ||
console.log(JSON.stringify(data, null, 2)); | ||
} | ||
prompt(question) { | ||
@@ -67,0 +73,0 @@ return new Promise((resolve) => { |
@@ -44,3 +44,3 @@ import { AsyncLocalStorage } from "async_hooks"; | ||
createErrorWithIdentifier(id, message) { | ||
let idstr = 'unk'; | ||
let idstr = null; | ||
// @ts-ignore | ||
@@ -54,3 +54,3 @@ if (id.name) { | ||
} | ||
return new Error(message.replace('%id', idstr)); | ||
return new Error(message.replace('%id', idstr || 'unk')); | ||
} | ||
@@ -57,0 +57,0 @@ /** |
@@ -62,4 +62,4 @@ import { AsyncLocalStorage } from 'node:async_hooks'; | ||
// put view provider in context | ||
ctx.set(HTTP_KEY_REQ_ID, reqid); | ||
ctx.set(HTTP_KEY_VIEW_PROVIDER, this.viewProvider); | ||
ctx.put(HTTP_KEY_REQ_ID, reqid); | ||
ctx.put(HTTP_KEY_VIEW_PROVIDER, this.viewProvider); | ||
// execute application middlewares | ||
@@ -77,2 +77,8 @@ await this.composedMiddleware(ctx, async (ctx, next) => { | ||
}); | ||
if (ctx.aborted && (this.viewProvider && ctx.accepts('html'))) { | ||
ctx.reply({ | ||
status: ctx.response.status, | ||
body: await this.viewProvider.renderError(ctx, { status: ctx.response.status, ...ctx.response.body }), | ||
}); | ||
} | ||
} | ||
@@ -86,6 +92,4 @@ catch (err) { | ||
const resultError = ResultError.try(err); | ||
if (ctx.accepts('html')) { | ||
if (this.viewProvider) { | ||
await this.viewProvider.renderError(ctx, resultError); | ||
} | ||
if (this.viewProvider && ctx.accepts('html')) { | ||
await this.viewProvider.renderError(ctx, resultError); | ||
} | ||
@@ -92,0 +96,0 @@ else { |
@@ -29,2 +29,4 @@ import { HttpRequestHeaders, HttpRequestQuery } from './context.js'; | ||
state = {}; | ||
response = { status: 404 }; | ||
aborted = false; | ||
_reply = null; | ||
@@ -85,12 +87,13 @@ constructor(ctx) { | ||
json(data, status = 200) { | ||
this.ctx.status = status; | ||
this.ctx.body = data; | ||
this.headers.set('content-type', 'application/json'); | ||
this.ctx.status = this.response.status = status; | ||
this.ctx.body = this.response.body = data; | ||
} | ||
noContent() { | ||
this.ctx.status = 204; | ||
this.ctx.body = undefined; | ||
this.ctx.status = this.response.status = 204; | ||
this.ctx.body = this.response.body = undefined; | ||
} | ||
created() { | ||
this.ctx.status = 201; | ||
this.ctx.body = undefined; | ||
this.ctx.status = this.response.status = 201; | ||
this.ctx.body = this.response.body = undefined; | ||
} | ||
@@ -101,21 +104,19 @@ throw(status, message) { | ||
abort(...args) { | ||
this.aborted = true; | ||
if (args.length == 1) { | ||
if (typeof args[0] == 'number') { | ||
this.ctx.status = args[0]; | ||
this.ctx.body = { message: 'Something went wrong.' }; | ||
this.ctx.status = this.response.status = args[0]; | ||
this.ctx.body = this.response.body = { message: 'Something went wrong.' }; | ||
return; | ||
} | ||
const err = args[0]; | ||
this.ctx.status = err.status; | ||
this.ctx.body = { message: err.message, ...(err.data || {}) }; | ||
this.ctx.status = this.response.status = err.status; | ||
this.ctx.body = this.response.body = { message: err.message, ...(err.data || {}) }; | ||
return; | ||
} | ||
this.ctx.status = args[0]; | ||
this.ctx.body = { message: args[1] || 'Something went wrong.', ...(args[2] || {}) }; | ||
this.ctx.status = this.response.status = args[0]; | ||
this.ctx.body = this.response.body = { message: args[1] || 'Something went wrong.', ...(args[2] || {}) }; | ||
} | ||
/** | ||
* ununsed | ||
* @param response | ||
*/ | ||
reply(response) { | ||
this.response = response; | ||
if (response.headers) { | ||
@@ -130,5 +131,5 @@ for (const key in response.headers) { | ||
stream(stream, mime = 'application/octet-stream') { | ||
this.ctx.status = 200; | ||
this.ctx.status = this.response.status = 200; | ||
this.ctx.type = mime; | ||
this.ctx.body = stream; | ||
this.ctx.body = this.response.body = stream; | ||
} | ||
@@ -143,3 +144,4 @@ redirect(url, alt) { | ||
} | ||
this.ctx.status = status; | ||
this.ctx.status = this.response.status = status; | ||
this.response.body = { template, data }; | ||
this.ctx.body = await view.render(this, template, data); | ||
@@ -154,5 +156,14 @@ this.willRender = true; | ||
// } | ||
/** | ||
* @deprecated use put() instead | ||
* @param key | ||
* @param value | ||
* @returns | ||
*/ | ||
set(key, value) { | ||
return this.state[key] = value; | ||
} | ||
put(key, value) { | ||
return this.state[key] = value; | ||
} | ||
value(key) { | ||
@@ -159,0 +170,0 @@ return this.state[key]; |
@@ -8,2 +8,5 @@ import { AsyncLocalStorage } from "async_hooks"; | ||
} | ||
put(key, value) { | ||
return this.state[key] = value; | ||
} | ||
value(key) { | ||
@@ -10,0 +13,0 @@ return this.state[key]; |
@@ -18,2 +18,3 @@ import { HttpCookies, HttpRequestHeaders, HttpRequestQuery } from '../../http/context.js'; | ||
}; | ||
aborted = false; | ||
_reply = null; | ||
@@ -123,2 +124,3 @@ parseHeaders(headers) { | ||
abort(...args) { | ||
this.aborted = true; | ||
if (args.length == 1) { | ||
@@ -171,2 +173,5 @@ if (typeof args[0] == 'number') { | ||
} | ||
put(key, value) { | ||
return this.state[key] = value; | ||
} | ||
value(key) { | ||
@@ -173,0 +178,0 @@ return this.state[key]; |
{ | ||
"name": "astad", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "astad is a nodejs framework for api and web development", | ||
@@ -5,0 +5,0 @@ "main": "./cjs/index.js", |
@@ -31,3 +31,5 @@ /// <reference types="node" /> | ||
info(message: string): void; | ||
error(message: string): void; | ||
infof(message: string, ...arg: any): void; | ||
json(data: any): void; | ||
prompt(question: string): Promise<unknown>; | ||
@@ -105,12 +107,12 @@ confirm(question: string): Promise<unknown>; | ||
} | ||
export interface ICommandFlag<T = any> extends ICommandArg<T> { | ||
alias: string; | ||
multiple: boolean; | ||
} | ||
export interface ICommandArg<T = any> { | ||
name: string; | ||
default: T; | ||
default?: T; | ||
value?: T; | ||
type: Boolean | Number | String; | ||
type?: Boolean | Number | String; | ||
} | ||
export interface ICommandFlag<T = any> extends ICommandArg<T> { | ||
alias?: string; | ||
multiple?: boolean; | ||
} | ||
export interface ICliMiddleware { | ||
@@ -117,0 +119,0 @@ handle(ctx: any, next: any): Promise<any>; |
@@ -37,2 +37,4 @@ /// <reference types="node" /> | ||
ips: string[]; | ||
response: IHttpResponse; | ||
aborted: boolean; | ||
is(...types: string[]): string | null | false; | ||
@@ -51,3 +53,9 @@ accepts(...types: string[]): string | false; | ||
redirect(url: string, alt?: string): void; | ||
/** | ||
* @deprecated use put() instead | ||
* @param key | ||
* @param value | ||
*/ | ||
set<T = any>(key: any, value: T): T; | ||
put<T = any>(key: any, value: T): T; | ||
value<T = any>(key: any): T | undefined; | ||
@@ -54,0 +62,0 @@ } |
@@ -28,2 +28,4 @@ /// <reference types="node" /> | ||
state: Record<any, any>; | ||
response: IHttpResponse<any>; | ||
aborted: boolean; | ||
protected _reply: IHttpResponse | null; | ||
@@ -52,6 +54,2 @@ constructor(ctx: any); | ||
abort(status: number, message?: string): void; | ||
/** | ||
* ununsed | ||
* @param response | ||
*/ | ||
reply(response: IHttpResponse): void; | ||
@@ -62,4 +60,11 @@ stream(stream: internal.Readable, mime?: string): void; | ||
shouldRender(): boolean; | ||
/** | ||
* @deprecated use put() instead | ||
* @param key | ||
* @param value | ||
* @returns | ||
*/ | ||
set<T = any>(key: any, value: T): T; | ||
put<T = any>(key: any, value: T): T; | ||
value<T = any>(key: any): T | undefined; | ||
} |
@@ -10,3 +10,7 @@ type ResponseHeaders = Record<string, string | string[]>; | ||
status: number; | ||
headers?: Record<string, string>; | ||
headers?: Record<string, string | string[]>; | ||
redirect?: { | ||
url: string; | ||
alt?: string; | ||
}; | ||
} | ||
@@ -13,0 +17,0 @@ export declare class HttpResponse { |
@@ -7,4 +7,5 @@ /// <reference types="node" /> | ||
set<T = any>(key: any, value: T): T; | ||
put<T = any>(key: any, value: T): T; | ||
value<T = any>(key: any): T | undefined; | ||
run(cb: () => any): any; | ||
} |
@@ -16,2 +16,3 @@ /// <reference types="node" /> | ||
response: ITestHttpResponse; | ||
aborted: boolean; | ||
protected _reply: IHttpResponse | null; | ||
@@ -53,3 +54,4 @@ parseHeaders(headers: any): any; | ||
set<T = any>(key: any, value: T): T; | ||
put<T = any>(key: any, value: T): T; | ||
value<T = any>(key: any): T | undefined; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
403780
6953