@compassdigital/sdk.typescript 3.0.0-beta.4 → 3.0.0-beta.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -5
- package/gen.ts +5 -6
- package/lib/base.d.ts +13 -1
- package/lib/base.d.ts.map +1 -1
- package/lib/base.js +45 -3
- package/lib/base.js.map +1 -1
- package/lib/index.d.ts +307 -306
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js.map +1 -1
- package/manifest.json +25 -50
- package/package.json +4 -2
- package/src/base.ts +29 -3
- package/src/index.ts +729 -611
- package/template.ejs +3 -2
package/README.md
CHANGED
|
@@ -2,8 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
Compass Digital Labs TypeScript SDK
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Install
|
|
6
6
|
|
|
7
|
+
```
|
|
8
|
+
$ npm install --save @compassdigital/sdk.typescript@latest
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Example Client Usage
|
|
7
12
|
|
|
8
13
|
``` typescript
|
|
9
14
|
import { ServiceClient } from "@compassdigital/sdk.typescript";
|
|
@@ -18,13 +23,12 @@ async function main() {
|
|
|
18
23
|
## Example Server Usage
|
|
19
24
|
|
|
20
25
|
``` typescript
|
|
21
|
-
import
|
|
22
|
-
import {
|
|
23
|
-
import { NextFn } from "@compassdigital/provider/interface"
|
|
26
|
+
import Provider from "@compassdigital/provider";
|
|
27
|
+
import { GetTaskRequest, GetTaskResponse } from "@compassdigital/sdk.typescript/task";
|
|
24
28
|
|
|
25
29
|
const provider = new Provider({ type: "payment" });
|
|
26
30
|
|
|
27
|
-
provider.on("
|
|
31
|
+
provider.on<GetTaskRequest, GetTaskResponse>("get_task", function (req, next): void {
|
|
28
32
|
// ...
|
|
29
33
|
})
|
|
30
34
|
```
|
package/gen.ts
CHANGED
|
@@ -20,7 +20,6 @@ interface Manifest {
|
|
|
20
20
|
interface ManifestService {
|
|
21
21
|
name: string; // service name
|
|
22
22
|
swagger: string; // swagger url or file
|
|
23
|
-
output: string; // typescript output file
|
|
24
23
|
}
|
|
25
24
|
|
|
26
25
|
interface ClientImport {
|
|
@@ -78,9 +77,8 @@ class CodeGenerator {
|
|
|
78
77
|
const details = analyse(doc);
|
|
79
78
|
this.transform(details, print, service);
|
|
80
79
|
const code = prettier.format(print.code(), { parser: "typescript", printWidth: 100 });
|
|
81
|
-
|
|
82
|
-
fs.
|
|
83
|
-
fs.writeFileSync(service.output, code);
|
|
80
|
+
const filename = path.join("src", `${service.name}.ts`);
|
|
81
|
+
fs.writeFileSync(filename, code);
|
|
84
82
|
}
|
|
85
83
|
// generate client
|
|
86
84
|
const print = new Printer();
|
|
@@ -98,7 +96,8 @@ class CodeGenerator {
|
|
|
98
96
|
const template = fs.readFileSync("template.ejs", "utf-8");
|
|
99
97
|
print.raw(ejs.render(template, { methods: this.methods }));
|
|
100
98
|
const code = prettier.format(print.code(), { parser: "typescript", printWidth: 100 });
|
|
101
|
-
|
|
99
|
+
const filename = path.join("src", "index.ts");
|
|
100
|
+
fs.writeFileSync(filename, code);
|
|
102
101
|
}
|
|
103
102
|
|
|
104
103
|
/**
|
|
@@ -234,7 +233,7 @@ class CodeGenerator {
|
|
|
234
233
|
}
|
|
235
234
|
options.setProperty("query", query);
|
|
236
235
|
}
|
|
237
|
-
options.merge(new Schema("
|
|
236
|
+
options.merge(new Schema("Options"));
|
|
238
237
|
method.params.push({
|
|
239
238
|
name: "options",
|
|
240
239
|
type: Printer.type(options),
|
package/lib/base.d.ts
CHANGED
|
@@ -5,10 +5,22 @@ export interface RequestOptions {
|
|
|
5
5
|
debug?: boolean;
|
|
6
6
|
retry?: number;
|
|
7
7
|
base_url?: string;
|
|
8
|
+
allow_404?: true;
|
|
9
|
+
}
|
|
10
|
+
export interface ServiceClientOptions extends Omit<RequestOptions, "allow_404"> {
|
|
11
|
+
}
|
|
12
|
+
export declare type ResponseType<T, Options extends RequestOptions> = Promise<T | (Options["allow_404"] extends true ? null : never)>;
|
|
13
|
+
export declare class ServiceError extends Error {
|
|
14
|
+
status: number;
|
|
15
|
+
code: number;
|
|
16
|
+
error: string;
|
|
17
|
+
constructor(status: number, // http status
|
|
18
|
+
code: number, // service error code
|
|
19
|
+
error: string);
|
|
8
20
|
}
|
|
9
21
|
export declare abstract class BaseServiceClient {
|
|
10
22
|
private options;
|
|
11
|
-
constructor(options?:
|
|
23
|
+
constructor(options?: ServiceClientOptions);
|
|
12
24
|
/**
|
|
13
25
|
* Combine the route and query parameters into a full url.
|
|
14
26
|
*/
|
package/lib/base.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,cAAc;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,cAAc;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,IAAI,CAAC;CAClB;AAED,MAAM,WAAW,oBAAqB,SAAQ,IAAI,CAAC,cAAc,EAAE,WAAW,CAAC;CAAG;AAElF,oBAAY,YAAY,CAAC,CAAC,EAAE,OAAO,SAAS,cAAc,IAAI,OAAO,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,CAAC,SAAS,IAAI,GAAG,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC;AAE9H,qBAAa,YAAa,SAAQ,KAAK;IAE5B,MAAM,EAAE,MAAM;IACd,IAAI,EAAE,MAAM;IACZ,KAAK,EAAE,MAAM;gBAFb,MAAM,EAAE,MAAM,EAAE,cAAc;IAC9B,IAAI,EAAE,MAAM,EAAI,qBAAqB;IACrC,KAAK,EAAE,MAAM;CAIvB;AAED,8BAAsB,iBAAiB;IACrC,OAAO,CAAC,OAAO,CAAuB;gBAE1B,OAAO,CAAC,EAAE,oBAAoB;IAI1C;;OAEG;IACH,OAAO,CAAC,SAAS;IAoBjB,OAAO,CAAC,MAAM;cAIE,OAAO,CACrB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,GAAG,EACT,OAAO,GAAE;QAAE,KAAK,CAAC,EAAE,GAAG,CAAA;KAAE,GAAG,cAAmB,GAC7C,OAAO,CAAC,GAAG,CAAC;CA8ChB"}
|
package/lib/base.js
CHANGED
|
@@ -1,4 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
2
17
|
var __assign = (this && this.__assign) || function () {
|
|
3
18
|
__assign = Object.assign || function(t) {
|
|
4
19
|
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
@@ -50,8 +65,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
50
65
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
66
|
};
|
|
52
67
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
-
exports.BaseServiceClient = void 0;
|
|
68
|
+
exports.BaseServiceClient = exports.ServiceError = void 0;
|
|
54
69
|
var cross_fetch_1 = __importDefault(require("cross-fetch"));
|
|
70
|
+
var ServiceError = /** @class */ (function (_super) {
|
|
71
|
+
__extends(ServiceError, _super);
|
|
72
|
+
function ServiceError(status, // http status
|
|
73
|
+
code, // service error code
|
|
74
|
+
error // service error message
|
|
75
|
+
) {
|
|
76
|
+
var _this = _super.call(this, code + ": " + error) || this;
|
|
77
|
+
_this.status = status;
|
|
78
|
+
_this.code = code;
|
|
79
|
+
_this.error = error;
|
|
80
|
+
return _this;
|
|
81
|
+
}
|
|
82
|
+
return ServiceError;
|
|
83
|
+
}(Error));
|
|
84
|
+
exports.ServiceError = ServiceError;
|
|
55
85
|
var BaseServiceClient = /** @class */ (function () {
|
|
56
86
|
function BaseServiceClient(options) {
|
|
57
87
|
this.options = options !== null && options !== void 0 ? options : {};
|
|
@@ -87,7 +117,7 @@ var BaseServiceClient = /** @class */ (function () {
|
|
|
87
117
|
var _a;
|
|
88
118
|
if (options === void 0) { options = {}; }
|
|
89
119
|
return __awaiter(this, void 0, void 0, function () {
|
|
90
|
-
var url, headers, req, res, text, data;
|
|
120
|
+
var url, headers, req, res, text, err, data_1, data;
|
|
91
121
|
return __generator(this, function (_b) {
|
|
92
122
|
switch (_b.label) {
|
|
93
123
|
case 0:
|
|
@@ -111,6 +141,9 @@ var BaseServiceClient = /** @class */ (function () {
|
|
|
111
141
|
case 1:
|
|
112
142
|
res = _b.sent();
|
|
113
143
|
if (!!res.ok) return [3 /*break*/, 3];
|
|
144
|
+
if (options.allow_404 && res.status === 404) {
|
|
145
|
+
return [2 /*return*/, null];
|
|
146
|
+
}
|
|
114
147
|
return [4 /*yield*/, res.text()];
|
|
115
148
|
case 2:
|
|
116
149
|
text = _b.sent();
|
|
@@ -121,7 +154,16 @@ var BaseServiceClient = /** @class */ (function () {
|
|
|
121
154
|
options.retry--;
|
|
122
155
|
return [2 /*return*/, this.request(method, route, body, options)];
|
|
123
156
|
}
|
|
124
|
-
|
|
157
|
+
err = new ServiceError(res.status, res.status, text);
|
|
158
|
+
try {
|
|
159
|
+
data_1 = JSON.parse(text);
|
|
160
|
+
if (data_1.code && data_1.error) {
|
|
161
|
+
err.code = data_1.code;
|
|
162
|
+
err.error = data_1.error;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
catch (_) { }
|
|
166
|
+
throw err;
|
|
125
167
|
case 3: return [4 /*yield*/, res.json()];
|
|
126
168
|
case 4:
|
|
127
169
|
data = _b.sent();
|
package/lib/base.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,4DAAgC;AAgBhC;IAAkC,gCAAK;IACrC,sBACS,MAAc,EAAE,cAAc;IAC9B,IAAY,EAAI,qBAAqB;IACrC,KAAa,CAAG,wBAAwB;;QAHjD,YAKE,kBAAS,IAAI,UAAK,KAAO,CAAC,SAC3B;QALQ,YAAM,GAAN,MAAM,CAAQ;QACd,UAAI,GAAJ,IAAI,CAAQ;QACZ,WAAK,GAAL,KAAK,CAAQ;;IAGtB,CAAC;IAC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