fulton-server
Advanced tools
Comparing version 0.0.14 to 0.0.15
@@ -12,12 +12,12 @@ import { FultonErrorObject, FultonErrorConstraints } from '../interfaces'; | ||
export declare class FultonError { | ||
errors: FultonErrorObject; | ||
error: FultonErrorObject; | ||
constructor(input?: FultonErrorObject | string); | ||
setMessage(msg: string): this; | ||
addError(propertyName: string, errorMessage: string, constraints?: FultonErrorConstraints): FultonError; | ||
addErrors(propertyName: string, constraints?: FultonErrorConstraints): FultonError; | ||
addDetail(propertyName: string, errorMessage: string, constraints?: FultonErrorConstraints): FultonError; | ||
addDetail(propertyName: string, constraints?: FultonErrorConstraints): FultonError; | ||
verifyRequired(target: any, propertyName: string, errorMessages?: string): boolean; | ||
verifyRequired(target: any, propertyNames: string[], errorMessages?: string[]): boolean; | ||
hasErrors(): boolean; | ||
hasError(): boolean; | ||
toJSON(): { | ||
errors: FultonErrorObject; | ||
error: FultonErrorObject; | ||
}; | ||
@@ -38,3 +38,3 @@ } | ||
add(errorMessage: string, addNameToMessage?: boolean): FultonStackError; | ||
hasErrors(): boolean; | ||
hasError(): boolean; | ||
} |
@@ -16,3 +16,3 @@ "use strict"; | ||
if (typeof input == "string") { | ||
this.errors = { | ||
this.error = { | ||
message: input | ||
@@ -22,27 +22,26 @@ }; | ||
else { | ||
this.errors = input || {}; | ||
this.error = input || {}; | ||
} | ||
} | ||
setMessage(msg) { | ||
this.errors.message = msg; | ||
this.error.message = msg; | ||
return this; | ||
} | ||
addError(propertyName, errorMessage, constraints) { | ||
if (this.errors.detail == null) { | ||
this.errors.detail = {}; | ||
addDetail(propertyName, ...args) { | ||
let errorMessage, constraints; | ||
if (args.length == 1) { | ||
constraints = args[0]; | ||
} | ||
let error = this.errors.detail[propertyName]; | ||
if (error && "constraints" in error) { | ||
Object.assign(error.constraints, constraints); | ||
else if (args.length == 2) { | ||
errorMessage = args[0]; | ||
constraints = args[1]; | ||
} | ||
else { | ||
this.errors.detail[propertyName] = { message: errorMessage, constraints }; | ||
// not support | ||
return; | ||
} | ||
return this; | ||
} | ||
addErrors(propertyName, constraints) { | ||
if (this.errors.detail == null) { | ||
this.errors.detail = {}; | ||
if (this.error.detail == null) { | ||
this.error.detail = {}; | ||
} | ||
let error = this.errors.detail[propertyName]; | ||
let error = this.error.detail[propertyName]; | ||
if (error) { | ||
@@ -52,3 +51,8 @@ Object.assign(error, constraints); | ||
else { | ||
this.errors.detail[propertyName] = constraints; | ||
if (errorMessage) { | ||
this.error.detail[propertyName] = { message: errorMessage, constraints }; | ||
} | ||
else { | ||
this.error.detail[propertyName] = constraints; | ||
} | ||
} | ||
@@ -70,3 +74,3 @@ return this; | ||
if (!lodash.some(target[propertyName])) { | ||
this.addError(propertyName, errorMessage || `${propertyName} is required`); | ||
this.addDetail(propertyName, errorMessage || `${propertyName} is required`); | ||
return false; | ||
@@ -77,8 +81,8 @@ } | ||
} | ||
hasErrors() { | ||
return this.errors.message != null || this.errors.detail != null; | ||
hasError() { | ||
return this.error.message != null || this.error.detail != null; | ||
} | ||
toJSON() { | ||
return { | ||
errors: this.errors | ||
error: this.error | ||
}; | ||
@@ -122,4 +126,4 @@ } | ||
add(errorMessage, addNameToMessage) { | ||
if (this.errors.detail == null) { | ||
this.errors.detail = {}; | ||
if (this.error.detail == null) { | ||
this.error.detail = {}; | ||
} | ||
@@ -130,9 +134,9 @@ let propertyName = this.properties.join("."); | ||
} | ||
this.errors.detail[propertyName] = errorMessage; | ||
this.error.detail[propertyName] = errorMessage; | ||
return this; | ||
} | ||
hasErrors() { | ||
return this.errors.detail != null; | ||
hasError() { | ||
return this.error.detail != null; | ||
} | ||
} | ||
exports.FultonStackError = FultonStackError; |
@@ -149,3 +149,3 @@ "use strict"; | ||
let entity = this.convertEntity(metadata, input, errorTracker); | ||
if (errorTracker.hasErrors()) { | ||
if (errorTracker.hasError()) { | ||
return Promise.reject(errorTracker); | ||
@@ -159,3 +159,3 @@ } | ||
if (errors.length == 0) { | ||
if (errorTracker.hasErrors()) { | ||
if (errorTracker.hasError()) { | ||
return Promise.reject(errorTracker); | ||
@@ -187,3 +187,3 @@ } | ||
return { | ||
errors: { | ||
error: { | ||
message: error.message | ||
@@ -269,3 +269,3 @@ } | ||
if (error.constraints) { | ||
fultonError.addErrors(property, error.constraints); | ||
fultonError.addDetail(property, error.constraints); | ||
} | ||
@@ -272,0 +272,0 @@ } |
@@ -18,5 +18,5 @@ "use strict"; | ||
find(repository, queryParams) { | ||
let errors = this.adjustParams(repository.metadata, queryParams); | ||
if (errors) { | ||
return Promise.reject(errors); | ||
let error = this.adjustParams(repository.metadata, queryParams); | ||
if (error) { | ||
return Promise.reject(error); | ||
} | ||
@@ -27,5 +27,5 @@ debug_1.fultonDebug("entity", "find on %s QueryParams:\n %O\t", repository.metadata.name, queryParams); | ||
findOne(repository, queryParams) { | ||
let errors = this.adjustParams(repository.metadata, queryParams); | ||
if (errors) { | ||
return Promise.reject(errors); | ||
let error = this.adjustParams(repository.metadata, queryParams); | ||
if (error) { | ||
return Promise.reject(error); | ||
} | ||
@@ -36,5 +36,5 @@ debug_1.fultonDebug("entity", "findOne on %s QueryParams:\n %O\t", repository.metadata.name, queryParams); | ||
count(repository, queryParams) { | ||
let errors = this.adjustParams(repository.metadata, queryParams, true); | ||
if (errors) { | ||
return Promise.reject(errors); | ||
let error = this.adjustParams(repository.metadata, queryParams, true); | ||
if (error) { | ||
return Promise.reject(error); | ||
} | ||
@@ -74,3 +74,3 @@ debug_1.fultonDebug("entity", "count on %s QueryParams:\n %O\t", repository.metadata.name, queryParams); | ||
delete params.needAdjust; | ||
if (errorTracker.hasErrors()) { | ||
if (errorTracker.hasError()) { | ||
return errorTracker; | ||
@@ -77,0 +77,0 @@ } |
@@ -53,3 +53,3 @@ "use strict"; | ||
if (data.length > 0 && queryParams.includes) { | ||
yield this.processIncludes(repo, data[0], queryParams.includes); | ||
yield this.processIncludes(repo, data, queryParams.includes); | ||
} | ||
@@ -56,0 +56,0 @@ return { data: data, total: count }; |
@@ -15,2 +15,3 @@ /// <reference types="express" /> | ||
export interface IFultonApp { | ||
readonly isInitialized: boolean; | ||
readonly appName: string; | ||
@@ -40,3 +41,3 @@ express: Express; | ||
readonly mode: AppMode; | ||
private isInitialized; | ||
private assetFolder; | ||
/** | ||
@@ -91,2 +92,3 @@ * app name, use in output, parser. default is class name. | ||
routers: Router[]; | ||
isInitialized: boolean; | ||
/** | ||
@@ -93,0 +95,0 @@ * @param mode There are some different default values for api and web-view. |
@@ -37,2 +37,3 @@ "use strict"; | ||
this.mode = mode; | ||
this.assetFolder = "./assets"; | ||
this.isInitialized = false; | ||
@@ -175,4 +176,4 @@ this.serve = (req, res) => { | ||
this.options.server.sslOptions = { | ||
cert: fs.readFileSync(path.join(__dirname, "../assets/ssl/localhost.crt")), | ||
key: fs.readFileSync(path.join(__dirname, "../assets/ssl/localhost.key")) | ||
cert: fs.readFileSync(path.join(__dirname, this.assetFolder, "ssl/localhost.crt")), | ||
key: fs.readFileSync(path.join(__dirname, this.assetFolder, "ssl/localhost.key")) | ||
}; | ||
@@ -179,0 +180,0 @@ fulton_log_1.FultonLog.warn(`${this.appName} is using dev ssl certification which is for development only, you should change sslOption for production`); |
@@ -16,4 +16,5 @@ import { IFultonApp } from './fulton-app'; | ||
private tasks; | ||
private lognTasks; | ||
constructor(App: Type<IFultonApp>); | ||
task(name: string, task: LaunchTask<TApp>): FultonAppLauncher<TApp>; | ||
task(name: string, task: LaunchTask<TApp>, isLongTask?: boolean): FultonAppLauncher<TApp>; | ||
/** | ||
@@ -23,4 +24,4 @@ * Launch Tasks based on process.env["{appName}.Launch"] | ||
*/ | ||
launch(tasks?: string[], stopAfterLaunch?: boolean): Promise<any>; | ||
launch(tasks?: string[]): Promise<any>; | ||
private appTask(app); | ||
} |
@@ -19,4 +19,5 @@ "use strict"; | ||
this.tasks = new Map(); | ||
this.lognTasks = new Map(); | ||
this.app = new App(); | ||
this.task("app", this.appTask); | ||
this.task("app", this.appTask, true); | ||
} | ||
@@ -26,4 +27,5 @@ static create(type) { | ||
} | ||
task(name, task) { | ||
task(name, task, isLongTask = false) { | ||
this.tasks.set(name, task); | ||
this.lognTasks.set(name, isLongTask); | ||
return this; | ||
@@ -35,3 +37,3 @@ } | ||
*/ | ||
launch(tasks, stopAfterLaunch) { | ||
launch(tasks) { | ||
if (tasks == null) { | ||
@@ -48,2 +50,3 @@ if (process.argv.length > 2) { | ||
debug_1.fultonDebug("launcher", `Start Tasks : ${tasks}`); | ||
let longTask = false; | ||
let promise = this.app.init().then(() => { | ||
@@ -53,5 +56,2 @@ return Promise.all(tasks.map((taskName) => { | ||
let task = this.tasks.get(taskName); | ||
if (stopAfterLaunch == null && taskName == "app") { | ||
stopAfterLaunch = false; | ||
} | ||
if (task == null) { | ||
@@ -61,2 +61,3 @@ fulton_log_1.FultonLog.error(`The launcher doesn't have task name called ${taskName}`); | ||
} | ||
longTask = longTask || this.lognTasks.get(taskName); | ||
debug_1.fultonDebug("launcher", `Starting Task : ${taskName}`); | ||
@@ -68,3 +69,3 @@ return task.call(this, this.app); | ||
debug_1.fultonDebug("launcher", `Tasks Started`); | ||
if (stopAfterLaunch == true || stopAfterLaunch == null) { | ||
if (longTask == false) { | ||
debug_1.fultonDebug("launcher", `App Stopping`); | ||
@@ -71,0 +72,0 @@ return this.app.stop(); |
@@ -89,3 +89,3 @@ export interface JsonApiConverterOptions { | ||
data?: JsonApiData | JsonApiData[]; | ||
errors?: any; | ||
errors?: JsonApiError[]; | ||
links?: JsonApiRootLinks; | ||
@@ -95,2 +95,6 @@ included?: JsonApiData[]; | ||
} | ||
export interface JsonApiError { | ||
message: string; | ||
detail?: any; | ||
} | ||
export declare class JsonApiConverter { | ||
@@ -97,0 +101,0 @@ options: JsonApiConverterOptions; |
@@ -14,2 +14,3 @@ "use strict"; | ||
const fulton_error_1 = require("../../common/fulton-error"); | ||
const fulton_log_1 = require("../../fulton-log"); | ||
/** | ||
@@ -28,2 +29,3 @@ * Default Fulton Implements | ||
}).catch((error) => { | ||
fulton_log_1.FultonLog.warn("login failed by", error); | ||
done(error); | ||
@@ -47,2 +49,3 @@ }); | ||
catch (error) { | ||
fulton_log_1.FultonLog.warn("loginByAccessToken failed by", error); | ||
return done(null, false); | ||
@@ -49,0 +52,0 @@ } |
@@ -31,2 +31,3 @@ "use strict"; | ||
const common_1 = require("../../common"); | ||
const bson_1 = require("bson"); | ||
class MongoRunner { | ||
@@ -92,9 +93,9 @@ constructor(userRepository) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let errors = new common_1.FultonError(); | ||
let error = new common_1.FultonError(); | ||
let registerOptions = this.options.register; | ||
// verify username, password, email | ||
errors.verifyRequired(input, ["username", "email"]); | ||
error.verifyRequired(input, ["username", "email"]); | ||
if (!input.oauthToken) { | ||
// if oauth register, no need password. | ||
errors.verifyRequired(input, "password"); | ||
error.verifyRequired(input, "password"); | ||
let pwResult; | ||
@@ -108,3 +109,3 @@ if (registerOptions.passwordVerifier instanceof Function) { | ||
if (!pwResult) { | ||
errors.addError("password", "password is invalid"); | ||
error.addDetail("password", "password is invalid"); | ||
} | ||
@@ -120,11 +121,11 @@ // if oauth register, skip username verify. | ||
if (!unResult) { | ||
errors.addError("username", "username is invalid"); | ||
error.addDetail("username", "username is invalid"); | ||
} | ||
} | ||
if (!validator.isEmail(input.email)) { | ||
errors.addError("email", "email is invalid"); | ||
error.addDetail("email", "email is invalid"); | ||
} | ||
if (errors.hasErrors()) { | ||
errors.setMessage("register failed"); | ||
throw errors; | ||
if (error.hasError()) { | ||
error.setMessage("register failed"); | ||
throw error; | ||
} | ||
@@ -140,3 +141,3 @@ input.email = input.email.toLocaleLowerCase(); | ||
if (count > 0) { | ||
errors.addError("username", "the username is existed"); | ||
error.addDetail("username", "the username is existed"); | ||
} | ||
@@ -147,7 +148,7 @@ count = yield this.userRepository.count({ | ||
if (count > 0) { | ||
errors.addError("email", "the email is existed"); | ||
error.addDetail("email", "the email is existed"); | ||
} | ||
if (errors.hasErrors()) { | ||
errors.setMessage("register failed"); | ||
throw errors; | ||
if (error.hasError()) { | ||
error.setMessage("register failed"); | ||
throw error; | ||
} | ||
@@ -172,12 +173,12 @@ if (input.oauthToken) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let errors = new common_1.FultonError(); | ||
let error = new common_1.FultonError(); | ||
if (!lodash.some(username)) { | ||
errors.addError("username", "username is required"); | ||
error.addDetail("username", "username is required"); | ||
} | ||
if (!lodash.some(password)) { | ||
errors.addError("password", "password is required"); | ||
error.addDetail("password", "password is required"); | ||
} | ||
if (errors.hasErrors()) { | ||
errors.setMessage("login failed"); | ||
throw errors; | ||
if (error.hasError()) { | ||
error.setMessage("login failed"); | ||
throw error; | ||
} | ||
@@ -191,3 +192,3 @@ let user = yield this.userRepository.findOne({ | ||
else { | ||
throw errors.setMessage("username or password isn't correct"); | ||
throw error.setMessage("username or password isn't correct"); | ||
} | ||
@@ -198,6 +199,6 @@ }); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let errors = new common_1.FultonError(); | ||
let error = new common_1.FultonError(); | ||
// verify email | ||
if (!errors.verifyRequired(profile, "email")) { | ||
throw errors; | ||
if (!error.verifyRequired(profile, "email")) { | ||
throw error; | ||
} | ||
@@ -245,2 +246,6 @@ profile.email = profile.email.toLocaleLowerCase(); | ||
payload = JSON.parse(json); | ||
if (payload.id && bson_1.ObjectId.isValid(payload.id)) { | ||
// convert id to ObjectId | ||
payload.id = new bson_1.ObjectId(payload.id); | ||
} | ||
} | ||
@@ -247,0 +252,0 @@ else { |
@@ -403,3 +403,3 @@ "use strict"; | ||
properties: { | ||
errors: { | ||
error: { | ||
type: "object", | ||
@@ -406,0 +406,0 @@ properties: { |
@@ -91,3 +91,3 @@ import "reflect-metadata"; | ||
status?: number; | ||
errors?: FultonErrorObject; | ||
error?: FultonErrorObject; | ||
} | ||
@@ -94,0 +94,0 @@ export interface OperationResultPagination { |
@@ -30,32 +30,39 @@ "use strict"; | ||
apply: (send, thisArg, args) => { | ||
if (args && args.length > 0 && args[0].data) { | ||
let body = args[0]; | ||
let data = body.data; | ||
let entityType; | ||
if (data instanceof Array) { | ||
if (data.length > 0) { | ||
entityType = data[0].constructor; | ||
} | ||
} | ||
else { | ||
entityType = data.constructor; | ||
} | ||
if (entityType) { | ||
res.set("content-type", constants_1.MimeTypes.jsonApi); | ||
let serializeOpts = { | ||
baseUrl: req.baseUrl, | ||
args: { | ||
queryParams: req.queryParams, | ||
pagination: body.pagination | ||
if (args && args.length == 1 && typeof args[0] == "object") { | ||
if (args[0].data) { | ||
let body = args[0]; | ||
let data = body.data; | ||
let entityType; | ||
if (data instanceof Array) { | ||
if (data.length > 0) { | ||
entityType = data[0].constructor; | ||
} | ||
}; | ||
if (converter.options.domain) { | ||
serializeOpts.domain = converter.options.domain; | ||
} | ||
else { | ||
serializeOpts.domain = `${req.protocol}://${req.get("host")}`; | ||
entityType = data.constructor; | ||
} | ||
let result = converter.serialize(entityType.name, data, serializeOpts); | ||
args[0] = result; | ||
if (entityType) { | ||
res.set("content-type", constants_1.MimeTypes.jsonApi); | ||
let serializeOpts = { | ||
baseUrl: req.baseUrl, | ||
args: { | ||
queryParams: req.queryParams, | ||
pagination: body.pagination | ||
} | ||
}; | ||
if (converter.options.domain) { | ||
serializeOpts.domain = converter.options.domain; | ||
} | ||
else { | ||
serializeOpts.domain = `${req.protocol}://${req.get("host")}`; | ||
} | ||
let result = converter.serialize(entityType.name, data, serializeOpts); | ||
args[0] = JSON.stringify(result); | ||
} | ||
} | ||
else if (args[0].error) { | ||
// JSONAPI use error array | ||
res.set("content-type", constants_1.MimeTypes.jsonApi); | ||
args[0] = JSON.stringify({ errors: [args[0].error] }); | ||
} | ||
} | ||
@@ -62,0 +69,0 @@ send.apply(thisArg, args); |
{ | ||
"name": "fulton-server", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"description": "Fulton is the best practical way to build web apis or websites we have done in our company. Basically, Fulton is integrated many popular libraries or frameworks seamlessly. By use Fulton, developers can build a completed web api or a websites quickly.", | ||
@@ -9,3 +9,3 @@ "main": "./main.js", | ||
"clean": "rimraf ./build", | ||
"build": "npm run clean && tsc -p tsconfig-prod.json && circular-require ./build/main.js", | ||
"build": "npm run clean && tsc -p tsconfig-prod.json && circular-require ./main.js", | ||
"build:test": "npm run clean && tsc -p tsconfig.json", | ||
@@ -83,3 +83,3 @@ "build:live": "npm run clean && tsc -w -p tsconfig-prod.json", | ||
"reflect-metadata": "^0.1.12", | ||
"typeorm": "^0.1.14", | ||
"typeorm": "^0.1.16", | ||
"url-join": "^4.0.0", | ||
@@ -86,0 +86,0 @@ "validator": "^9.4.1", |
@@ -70,3 +70,3 @@ "use strict"; | ||
res.status(400).send({ | ||
errors: { "message": "no data" } | ||
error: { "message": "no data" } | ||
}); | ||
@@ -84,3 +84,3 @@ } | ||
res.status(400).send({ | ||
errors: { "message": "no data or id" } | ||
error: { "message": "no data or id" } | ||
}); | ||
@@ -98,3 +98,3 @@ } | ||
res.status(400).send({ | ||
errors: { "message": "no id" } | ||
error: { "message": "no id" } | ||
}); | ||
@@ -105,3 +105,3 @@ } | ||
return (result) => { | ||
if (result.errors) { | ||
if (result.error) { | ||
res.status(400).send(result); | ||
@@ -119,3 +119,3 @@ } | ||
res.status(400).send({ | ||
errors: { | ||
error: { | ||
"message": "no data" | ||
@@ -122,0 +122,0 @@ } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
298625
7747
2
Updatedtypeorm@^0.1.16