const create = (apiResponse) => { if (apiResponse instanceof ApiResponse) { return apiResponse; } if (apiResponse instanceof Error) { return new ApiResponse({ error: apiResponse }); } if (is.undefined(apiResponse)) { return new ApiResponse(); } if (is.object(apiResponse) && Object.keys(apiResponse).length === 1) { const key = Object.keys(apiResponse)[0]; if (key === 'loading') { return new ApiResponse({ loading: apiResponse.loading === true }); } if (key === 'error') { if (is.undefined(apiResponse.error) || apiResponse.error instanceof Error) { return new ApiResponse({ error: apiResponse.error }); } return new ApiResponse({ error: new Error(apiResponse.error) }); } if (key === 'data') { return new ApiResponse({ data: apiResponse.data }); } } return new ApiResponse({ data: apiResponse }); };
Model.prototype.set = function (key, value) { // if object passed instead of key-value pair // iterate and call set on each item if (is.object(key)) { let attrs = key; let keys = Object.keys(attrs); let self = this; keys.forEach(function (key) { self.set(key, attrs[key]); }); return; } // check if the value actually changed let previousValue = this.get(key); if (previousValue !== value) { set(this.previous, key, previousValue); set(this.attributes, key, value); set(this.changed, key, true); } return value; };
SqlBuilder.prototype.set = function (keyValuesOrKvCount) { if (keyValuesOrKvCount) { var i, value; this.sql += 'SET '; if (is.object(keyValuesOrKvCount)) { for (value in keyValuesOrKvCount) { if (keyValuesOrKvCount.hasOwnProperty(value)) { this.sql += (value + ' = ' + keyValuesOrKvCount[value] + ', '); } } } else if (is.number(keyValuesOrKvCount)) { for (i = 0; i < keyValuesOrKvCount; i++) { this.sql += '?? = ?, '; } } this.sql = this.sql.substring(0, this.sql.length - ', '.length) + ' '; } return this; };
SqlBuilder.prototype.where = function (keyValuesOrKvCount, andOr) { if (keyValuesOrKvCount) { var i, value; this.sql += 'WHERE '; if (is.object(keyValuesOrKvCount)) { for (value in keyValuesOrKvCount) { if (keyValuesOrKvCount.hasOwnProperty(value)) { this.sql += (value + ' = ' + keyValuesOrKvCount[value] + ((andOr === 'AND') ? ' AND ' : ' OR ')); } } } else if (is.number(keyValuesOrKvCount)) { for (i = 0; i < keyValuesOrKvCount; i++) { this.sql += ('?? = ? ' + ((andOr === 'AND') ? ' AND ' : ' OR ')); } } this.sql = this.sql.substring(0, this.sql.length - ((andOr === 'AND') ? ' AND '.length : ' OR '.length)) + ' '; } return this; };
args.forEach(function (arg) { if (is.string(arg)) { urls.push(arg); } if (is.object(arg)) { options = arg; } });
SqlBuilder.prototype.orderBy = function (columnsAndOrder) { if (columnsAndOrder) { var column; this.sql += 'ORDER BY '; if (is.object(columnsAndOrder)) { for (column in columnsAndOrder) { if (columnsAndOrder.hasOwnProperty(column)) { this.sql += (column + ' ' + columnsAndOrder[column] + ', '); } } } this.sql = this.sql.substring(0, this.sql.length - ', '.length) + ' '; } return this; };
const isApiResponse = (apiResponse) => is.object(apiResponse) && apiResponse instanceof ApiResponse;
Model.prototype.hook = function (when, action, method) { let self = this; // if object is given // iterate and call .hook() // for each entry if (is.object(when)) { let hooks = when; let keys = Object.keys(hooks); keys.forEach(function (key) { let parts = key.split(':'); let when = parts[0]; let action = parts[1]; let method = hooks[key]; self.hook(when, action, method); }); return; } // if array is given // iterate and call .hook() // for each item if (Array.isArray(method)) { let methods = method; methods.forEach(function (method) { return self.hook(when, action, method); }); return; } // if method is a string // get the function if (is.not.function(method)) { let name = method; method = this[method]; if (!method.name) { method.name = name; } } // if method is a generator function // convert it to promise if (isGeneratorFn(method)) { method = co.wrap(method); } // around hooks should be // at the end of before:* // at the beginning of after:* if (when === 'around') { this._hooks.before[action].push(method); this._hooks.after[action].unshift(method); } else { this._hooks[when][action].push(method); } };
.then((payload) => { // レスポンスを閉じる res.end(is.object(payload) || is.array(payload) ? JSON.stringify(payload) : payload); });