@specter/specter
Advanced tools
Comparing version 0.3.0 to 0.4.0
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const specter_1 = __importDefault(require("./specter")); | ||
class SpecterClient { | ||
constructor(options) { | ||
var tslib_1 = require("tslib"); | ||
var specter_1 = tslib_1.__importDefault(require("./specter")); | ||
var SpecterClient = /** @class */ (function () { | ||
function SpecterClient(options) { | ||
this.options = options; | ||
} | ||
execute(request, restype) { | ||
SpecterClient.prototype.execute = function (request, restype) { | ||
if (!request.method) { | ||
@@ -16,29 +14,30 @@ throw new Error("Request method not found"); | ||
if (!specter_1.default.isRegistered(request.resource)) { | ||
throw new Error(`Service is not registered ${request.resource}`); | ||
throw new Error("Service is not registered " + request.resource); | ||
} | ||
const service = specter_1.default.getService(request.resource); | ||
const response = service.execute(request); | ||
var service = specter_1.default.getService(request.resource); | ||
var response = service.execute(request); | ||
return response; | ||
} | ||
create(request) { | ||
}; | ||
SpecterClient.prototype.create = function (request) { | ||
request.method = "POST"; | ||
return this.execute(request); | ||
} | ||
read(request) { | ||
}; | ||
SpecterClient.prototype.read = function (request) { | ||
request.method = "GET"; | ||
return this.execute(request); | ||
} | ||
update(request) { | ||
}; | ||
SpecterClient.prototype.update = function (request) { | ||
request.method = "PUT"; | ||
return this.execute(request); | ||
} | ||
delete(request) { | ||
}; | ||
SpecterClient.prototype.delete = function (request) { | ||
request.method = "DELETE"; | ||
return this.execute(request); | ||
} | ||
exists(request) { | ||
}; | ||
SpecterClient.prototype.exists = function (request) { | ||
request.method = "HEAD"; | ||
return this.execute(request); | ||
} | ||
} | ||
}; | ||
return SpecterClient; | ||
}()); | ||
exports.default = SpecterClient; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class SpecterNetworkError extends Error { | ||
constructor(message, status, statusText, request, response) { | ||
super(`Specter Error: ${message}`); | ||
this.isSpecterNetworkError = true; | ||
this.req = request; | ||
this.res = response; | ||
this.status = status; | ||
this.statusText = statusText; | ||
exports.NotSupportedMethod = exports.NotImplemented = exports.isSpecterNetworkError = exports.SpecterNetworkError = void 0; | ||
var tslib_1 = require("tslib"); | ||
var SpecterNetworkError = /** @class */ (function (_super) { | ||
tslib_1.__extends(SpecterNetworkError, _super); | ||
function SpecterNetworkError(message, status, statusText, request, response) { | ||
var _this = _super.call(this, "Specter Error: " + message) || this; | ||
_this.isSpecterNetworkError = true; | ||
_this.req = request; | ||
_this.res = response; | ||
_this.status = status; | ||
_this.statusText = statusText; | ||
return _this; | ||
} | ||
} | ||
return SpecterNetworkError; | ||
}(Error)); | ||
exports.SpecterNetworkError = SpecterNetworkError; | ||
@@ -20,4 +25,4 @@ function isSpecterNetworkError(err) { | ||
exports.isSpecterNetworkError = isSpecterNetworkError; | ||
class SpecterError { | ||
constructor(code, message, error) { | ||
var SpecterError = /** @class */ (function () { | ||
function SpecterError(code, message, error) { | ||
this.code = code; | ||
@@ -27,3 +32,4 @@ this.message = message; | ||
} | ||
} | ||
return SpecterError; | ||
}()); | ||
var SpecterErrorType; | ||
@@ -30,0 +36,0 @@ (function (SpecterErrorType) { |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const specter_1 = __importDefault(require("./specter")); | ||
const request_1 = __importDefault(require("./request")); | ||
exports.Client = exports.Service = exports.Response = exports.Request = void 0; | ||
var tslib_1 = require("tslib"); | ||
var specter_1 = tslib_1.__importDefault(require("./specter")); | ||
var request_1 = tslib_1.__importDefault(require("./request")); | ||
exports.Request = request_1.default; | ||
const response_1 = __importDefault(require("./response")); | ||
var response_1 = tslib_1.__importDefault(require("./response")); | ||
exports.Response = response_1.default; | ||
const service_1 = __importDefault(require("./service")); | ||
var service_1 = tslib_1.__importDefault(require("./service")); | ||
exports.Service = service_1.default; | ||
const client_1 = __importDefault(require("./client")); | ||
var client_1 = tslib_1.__importDefault(require("./client")); | ||
exports.Client = client_1.default; | ||
var errors_1 = require("./errors"); | ||
exports.SpecterNetworkError = errors_1.SpecterNetworkError; | ||
exports.isSpecterNetworkError = errors_1.isSpecterNetworkError; | ||
Object.defineProperty(exports, "SpecterNetworkError", { enumerable: true, get: function () { return errors_1.SpecterNetworkError; } }); | ||
Object.defineProperty(exports, "isSpecterNetworkError", { enumerable: true, get: function () { return errors_1.isSpecterNetworkError; } }); | ||
exports.default = specter_1.default; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class SpecterRequest { | ||
constructor(resource, req) { | ||
var SpecterRequest = /** @class */ (function () { | ||
function SpecterRequest(resource, req) { | ||
var _a; | ||
@@ -16,4 +16,4 @@ this.resource = resource; | ||
} | ||
static parseRequest(req) { | ||
const parsed = JSON.parse(req); | ||
SpecterRequest.parseRequest = function (req) { | ||
var parsed = JSON.parse(req); | ||
return new SpecterRequest(parsed.resource, { | ||
@@ -23,6 +23,6 @@ method: parsed.method, | ||
query: parsed.query, | ||
body: parsed.body | ||
body: parsed.body, | ||
}); | ||
} | ||
toString() { | ||
}; | ||
SpecterRequest.prototype.toString = function () { | ||
return JSON.stringify({ | ||
@@ -33,6 +33,7 @@ resource: this.resource, | ||
body: this.body, | ||
method: this.method | ||
method: this.method, | ||
}); | ||
} | ||
} | ||
}; | ||
return SpecterRequest; | ||
}()); | ||
exports.default = SpecterRequest; |
import SpecterRequest from "./request"; | ||
export declare type AnyResponse = SpecterResponse<{}, {}>; | ||
export default class SpecterResponse<H extends any, B extends any> { | ||
export default class SpecterResponse<H extends Record<string, any>, B extends any> { | ||
status?: number; | ||
@@ -5,0 +5,0 @@ headers: H; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class SpecterResponse { | ||
constructor(headers, body) { | ||
var tslib_1 = require("tslib"); | ||
var SpecterResponse = /** @class */ (function () { | ||
function SpecterResponse(headers, body) { | ||
this.headers = headers; | ||
this.body = body; | ||
} | ||
setStatus(status) { | ||
SpecterResponse.prototype.setStatus = function (status) { | ||
this.status = status; | ||
} | ||
setNextReqs(...reqs) { | ||
this.headers = { | ||
...this.headers, | ||
"x-specter-next-reqs": reqs.map(req => req.toString()).join("__sep__") | ||
}; | ||
}; | ||
SpecterResponse.prototype.setNextReqs = function () { | ||
var reqs = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
reqs[_i] = arguments[_i]; | ||
} | ||
this.headers = tslib_1.__assign(tslib_1.__assign({}, this.headers), { "x-specter-next-reqs": reqs.map(function (req) { return req.toString(); }).join("__sep__") }); | ||
this.nextReqs = reqs; | ||
} | ||
appendHeader(key, value) { | ||
this.headers[key] = value; | ||
} | ||
setError(error) { | ||
}; | ||
SpecterResponse.prototype.appendHeader = function (key, value) { | ||
var _a; | ||
this.headers = tslib_1.__assign(tslib_1.__assign({}, this.headers), (_a = {}, _a[key] = value, _a)); | ||
}; | ||
SpecterResponse.prototype.setError = function (error) { | ||
this.error = error; | ||
} | ||
getNextReqs() { | ||
}; | ||
SpecterResponse.prototype.getNextReqs = function () { | ||
return this.nextReqs; | ||
} | ||
static parse(res) { | ||
const parsed = JSON.parse(res); | ||
const response = new SpecterResponse(parsed.headers, parsed.body); | ||
}; | ||
SpecterResponse.parse = function (res) { | ||
var parsed = JSON.parse(res); | ||
var response = new SpecterResponse(parsed.headers, parsed.body); | ||
response.setStatus(parsed.status); | ||
return response; | ||
} | ||
toString() { | ||
}; | ||
SpecterResponse.prototype.toString = function () { | ||
return JSON.stringify({ | ||
status: this.status, | ||
headers: this.headers, | ||
body: this.body | ||
body: this.body, | ||
}); | ||
} | ||
} | ||
}; | ||
return SpecterResponse; | ||
}()); | ||
exports.default = SpecterResponse; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const errors_1 = require("./errors"); | ||
class Service { | ||
constructor(name, config) { | ||
var tslib_1 = require("tslib"); | ||
var errors_1 = require("./errors"); | ||
var Service = /** @class */ (function () { | ||
function Service(name, config) { | ||
this.name = name; | ||
this.config = config; | ||
} | ||
async execute(req) { | ||
const check = await this.preCheck(req); | ||
if (!check) { | ||
return Promise.reject(new Error("Precheck failed.")); | ||
} | ||
let res; | ||
if (req.method === "GET" && this.read) { | ||
res = await this.read(req); | ||
} | ||
else if (req.method === "POST" && this.create) { | ||
res = await this.create(req); | ||
} | ||
else if (req.method === "PUT" && this.update) { | ||
res = await this.update(req); | ||
} | ||
else if (req.method === "DELETE" && this.delete) { | ||
res = await this.delete(req); | ||
} | ||
else if (req.method === "HEAD" && this.exist) { | ||
res = await this.exist(req); | ||
} | ||
if (!res) { | ||
return Promise.reject(errors_1.NotSupportedMethod); | ||
} | ||
return res; | ||
} | ||
create(req) { | ||
Service.prototype.execute = function (req) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var check, res; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.preCheck(req)]; | ||
case 1: | ||
check = _a.sent(); | ||
if (!check) { | ||
return [2 /*return*/, Promise.reject(new Error("Precheck failed."))]; | ||
} | ||
if (!(req.method === "GET" && this.read)) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, this.read(req)]; | ||
case 2: | ||
res = _a.sent(); | ||
return [3 /*break*/, 11]; | ||
case 3: | ||
if (!(req.method === "POST" && this.create)) return [3 /*break*/, 5]; | ||
return [4 /*yield*/, this.create(req)]; | ||
case 4: | ||
res = _a.sent(); | ||
return [3 /*break*/, 11]; | ||
case 5: | ||
if (!(req.method === "PUT" && this.update)) return [3 /*break*/, 7]; | ||
return [4 /*yield*/, this.update(req)]; | ||
case 6: | ||
res = _a.sent(); | ||
return [3 /*break*/, 11]; | ||
case 7: | ||
if (!(req.method === "DELETE" && this.delete)) return [3 /*break*/, 9]; | ||
return [4 /*yield*/, this.delete(req)]; | ||
case 8: | ||
res = _a.sent(); | ||
return [3 /*break*/, 11]; | ||
case 9: | ||
if (!(req.method === "HEAD" && this.exist)) return [3 /*break*/, 11]; | ||
return [4 /*yield*/, this.exist(req)]; | ||
case 10: | ||
res = _a.sent(); | ||
_a.label = 11; | ||
case 11: | ||
if (!res) { | ||
return [2 /*return*/, Promise.reject(errors_1.NotSupportedMethod)]; | ||
} | ||
return [2 /*return*/, res]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Service.prototype.create = function (req) { | ||
return Promise.reject(errors_1.NotImplemented); | ||
} | ||
read(req) { | ||
}; | ||
Service.prototype.read = function (req) { | ||
return Promise.reject(errors_1.NotImplemented); | ||
} | ||
update(req) { | ||
}; | ||
Service.prototype.update = function (req) { | ||
return Promise.reject(errors_1.NotImplemented); | ||
} | ||
delete(req) { | ||
}; | ||
Service.prototype.delete = function (req) { | ||
return Promise.reject(errors_1.NotImplemented); | ||
} | ||
exist(req) { | ||
}; | ||
Service.prototype.exist = function (req) { | ||
return Promise.reject(errors_1.NotImplemented); | ||
} | ||
preCheck(req) { | ||
}; | ||
Service.prototype.preCheck = function (req) { | ||
return Promise.resolve(true); | ||
} | ||
} | ||
}; | ||
return Service; | ||
}()); | ||
exports.default = Service; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const url_1 = require("url"); | ||
const request_1 = __importDefault(require("./request")); | ||
const DefaultContentType = "application/json"; | ||
class Specter { | ||
static registerService(service) { | ||
var tslib_1 = require("tslib"); | ||
var url_1 = require("url"); | ||
var request_1 = tslib_1.__importDefault(require("./request")); | ||
var DefaultContentType = "application/json"; | ||
var Specter = /** @class */ (function () { | ||
function Specter() { | ||
} | ||
Specter.registerService = function (service) { | ||
Specter.services.set(service.name, service); | ||
} | ||
static getService(name) { | ||
const service = Specter.services.get(name); | ||
}; | ||
Specter.getService = function (name) { | ||
var service = Specter.services.get(name); | ||
if (!service) { | ||
throw new Error(`Service not found: ${name}`); | ||
throw new Error("Service not found: " + name); | ||
} | ||
return service; | ||
} | ||
static isRegistered(name) { | ||
}; | ||
Specter.isRegistered = function (name) { | ||
return Specter.services.has(name); | ||
} | ||
static createMiddleware(options) { | ||
return async (req, res, next) => { | ||
try { | ||
if (!req.url) { | ||
throw new Error("req url is not found."); | ||
}; | ||
Specter.createMiddleware = function (options) { | ||
var _this = this; | ||
return function (req, res, next) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var url, resource, request, service, response, headers, _i, _a, _b, key, value, e_1; | ||
return tslib_1.__generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
_c.trys.push([0, 2, , 3]); | ||
if (!req.url) { | ||
throw new Error("req url is not found."); | ||
} | ||
url = url_1.parse(req.url); | ||
if (!url.pathname) { | ||
throw new Error("url pathname is not found."); | ||
} | ||
resource = url.pathname.substr(1).split("/").shift(); | ||
if (!resource) { | ||
throw new Error("resource is not found."); | ||
} | ||
request = new request_1.default(resource, req); | ||
if (!Specter.isRegistered(request.resource)) { | ||
throw new Error("resource: " + resource + " is not registered."); | ||
} | ||
service = Specter.getService(request.resource); | ||
return [4 /*yield*/, service.execute(request)]; | ||
case 1: | ||
response = _c.sent(); | ||
headers = {}; | ||
for (_i = 0, _a = Object.entries(response.headers); _i < _a.length; _i++) { | ||
_b = _a[_i], key = _b[0], value = _b[1]; | ||
headers[key] = value; | ||
} | ||
headers["Content-Type"] = DefaultContentType; | ||
headers["access-control-expose-headers"] = Object.keys(headers).join(","); | ||
res.writeHead(response.status || 200, headers); | ||
if (response.body) { | ||
res.end(JSON.stringify(response.body)); | ||
} | ||
else { | ||
res.end(null); | ||
} | ||
return [3 /*break*/, 3]; | ||
case 2: | ||
e_1 = _c.sent(); | ||
next(e_1); | ||
return [3 /*break*/, 3]; | ||
case 3: return [2 /*return*/]; | ||
} | ||
const url = url_1.parse(req.url); | ||
if (!url.pathname) { | ||
throw new Error("url pathname is not found."); | ||
} | ||
const resource = url.pathname | ||
.substr(1) | ||
.split("/") | ||
.shift(); | ||
if (!resource) { | ||
throw new Error("resource is not found."); | ||
} | ||
const request = new request_1.default(resource, req); | ||
if (!Specter.isRegistered(request.resource)) { | ||
throw new Error(`resource: ${resource} is not registered.`); | ||
} | ||
const service = Specter.getService(request.resource); | ||
const response = await service.execute(request); | ||
const headers = {}; | ||
for (const [key, value] of Object.entries(response.headers)) { | ||
headers[key] = value; | ||
} | ||
headers["Content-Type"] = DefaultContentType; | ||
headers["access-control-expose-headers"] = Object.keys(headers).join(","); | ||
res.writeHead(response.status || 200, headers); | ||
if (response.body) { | ||
res.end(JSON.stringify(response.body)); | ||
} | ||
else { | ||
res.end(null); | ||
} | ||
} | ||
catch (e) { | ||
next(e); | ||
} | ||
return; | ||
}; | ||
} | ||
} | ||
}); | ||
}); }; | ||
}; | ||
Specter.services = new Map(); | ||
return Specter; | ||
}()); | ||
exports.default = Specter; | ||
Specter.services = new Map(); |
{ | ||
"name": "@specter/specter", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "> TODO: description", | ||
@@ -9,2 +9,4 @@ "author": "Yosuke Furukawa <yosuke.furukawa@gmail.com>", | ||
"main": "lib/index.js", | ||
"modules": "esm/index.js", | ||
"types": "lib/index.d.ts", | ||
"directories": { | ||
@@ -23,4 +25,6 @@ "lib": "lib", | ||
"test": "jest", | ||
"build": "tsc -d", | ||
"prepublish": "npm run build" | ||
"prepublishOnly": "npm run build", | ||
"build": "npm run build:esm && npm run build:cjs", | ||
"build:esm": "tsc -d -p ./tsconfig.json", | ||
"build:cjs": "tsc -d -p ./tsconfig.esm.json" | ||
}, | ||
@@ -31,3 +35,4 @@ "bugs": { | ||
"dependencies": { | ||
"@types/express": "4.17.1", | ||
"@types/express": "4.17.6", | ||
"tslib": "^2.0.0", | ||
"unfetch": "^4.1.0" | ||
@@ -39,6 +44,6 @@ }, | ||
"isomorphic-unfetch": "3.0.0", | ||
"jest": "25.5.4", | ||
"typescript": "3.8.3" | ||
"jest": "26.0.1", | ||
"typescript": "3.9.2" | ||
}, | ||
"gitHead": "f4ff1e14fb1a90a37e6d9299d67ab4607dc92d05", | ||
"gitHead": "ca8de075cb41331d38b1176777bba3536a02723c", | ||
"publishConfig": { | ||
@@ -45,0 +50,0 @@ "access": "public" |
20080
461
3
+ Addedtslib@^2.0.0
+ Added@types/express@4.17.6(transitive)
+ Addedtslib@2.8.1(transitive)
- Removed@types/express@4.17.1(transitive)
Updated@types/express@4.17.6