@pyrpc/types 0.6.0 → 0.6.2

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/package.json CHANGED
@@ -1,26 +1,24 @@
1
- {
2
- "name": "@pyrpc/types",
3
- "version": "0.6.0",
4
- "description": "pyRPC TypeScript type definitions - generated by pyrpc codegen",
5
- "exports": {
6
- ".": {
7
- "types": "./src/index.ts",
8
- "default": "./src/index.ts"
9
- }
10
- },
11
- "files": [
12
- "src",
13
- "postinstall.js"
14
- ],
15
- "scripts": {
16
- "postinstall": "node postinstall.js",
17
- "lint": "echo 'no lint'",
18
- "test": "echo 'no test'"
19
- },
20
- "keywords": [
21
- "pyrpc",
22
- "typescript",
23
- "types"
24
- ],
25
- "license": "MIT"
26
- }
1
+ {
2
+ "name": "@pyrpc/types",
3
+ "version": "0.6.2",
4
+ "description": "pyRPC TypeScript type definitions - generated by pyrpc codegen",
5
+ "exports": {
6
+ ".": {
7
+ "types": "./src/index.ts",
8
+ "default": "./src/index.ts"
9
+ }
10
+ },
11
+ "files": [
12
+ "src"
13
+ ],
14
+ "scripts": {
15
+ "lint": "echo 'no lint'",
16
+ "test": "echo 'no test'"
17
+ },
18
+ "keywords": [
19
+ "pyrpc",
20
+ "typescript",
21
+ "types"
22
+ ],
23
+ "license": "MIT"
24
+ }
package/src/index.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  // @pyrpc/types - types not generated yet.
2
2
  // Install @pyrpc/client and the postinstall script will prompt for setup:
3
3
  // npm install @pyrpc/client
4
- // Or generate manually:
5
- // pyrpc codegen <url>
4
+ // Or sync manually from a running server:
5
+ // npx pyrpc sync
6
6
  //
7
7
  // This file is replaced when codegen runs.
8
8
 
package/postinstall.js DELETED
@@ -1,172 +0,0 @@
1
- var fs = require('fs');
2
- var path = require('path');
3
- var readline = require('readline');
4
-
5
- var TYPES_FILE = path.join(__dirname, 'src', 'index.ts');
6
- var PLACEHOLDER_MARKER = 'types not generated yet';
7
-
8
- function findProjectRoot() {
9
- var dir = path.dirname(__dirname);
10
- while (true) {
11
- var pkgPath = path.join(dir, 'package.json');
12
- if (fs.existsSync(pkgPath) && !pkgPath.replace(/\\/g, '/').includes('/node_modules/')) {
13
- return dir;
14
- }
15
- var parent = path.dirname(dir);
16
- if (parent === dir) return process.cwd();
17
- dir = parent;
18
- }
19
- }
20
-
21
- function getConfigPath() {
22
- return path.join(findProjectRoot(), 'pyrpc-client.json');
23
- }
24
-
25
- var TYPE_MAP = {
26
- int: 'number',
27
- float: 'number',
28
- str: 'string',
29
- bool: 'boolean',
30
- None: 'null',
31
- NoneType: 'null',
32
- Any: 'any',
33
- };
34
-
35
- function toTs(t) {
36
- if (!t || t === 'None') return 'void';
37
- var m = t.match(/^<class\s+'([^'>]+)'>$/);
38
- if (m) {
39
- var n = m[1];
40
- if (TYPE_MAP[n]) return TYPE_MAP[n];
41
- if (n[0] >= 'A' && n[0] <= 'Z') return n;
42
- return 'any';
43
- }
44
- var s = t.replace(/^typing\./, '');
45
- var o = s.match(/^Optional\[(.+)\]$/);
46
- if (o) return toTs(o[1]) + ' | null';
47
- var l = s.match(/^(?:List|list)\[(.+)\]$/);
48
- if (l) return toTs(l[1]) + '[]';
49
- var d = s.match(/^(?:Dict|dict)\[([^,]+),\s*(.+)\]$/);
50
- if (d) return 'Record<' + toTs(d[1]) + ', ' + toTs(d[2]) + '>';
51
- var u = s.match(/^Union\[(.+)\]$/);
52
- if (u) return u[1].split(',').map(function(p) { return toTs(p.trim()); }).join(' | ');
53
- var tup = s.match(/^(?:Tuple|tuple)\[([^\]]+)\]$/);
54
- if (tup) return '[' + tup[1].split(',').map(function(p) { return toTs(p.trim()); }).join(', ') + ']';
55
- return 'any';
56
- }
57
-
58
- function generate(schemas) {
59
- var lines = [];
60
- lines.push('// Auto-generated by @pyrpc/types');
61
- lines.push('// Schema fetched from: ' + (process.env.PYRPC_URL || 'http://localhost:8000'));
62
- lines.push('');
63
- var names = Object.keys(schemas);
64
- for (var i = 0; i < names.length; i++) {
65
- var name = names[i];
66
- var schema = schemas[name];
67
- lines.push('export interface ' + name + 'Params {');
68
- var params = schema.parameters || [];
69
- for (var j = 0; j < params.length; j++) {
70
- var p = params[j];
71
- lines.push(' ' + p.name + ': ' + toTs(p.type) + ';');
72
- }
73
- lines.push('}');
74
- lines.push('');
75
- lines.push('export interface ' + name + 'Result {');
76
- lines.push(' data: ' + toTs(schema.return_type) + ';');
77
- lines.push('}');
78
- lines.push('');
79
- }
80
- lines.push('export interface Types {');
81
- for (var k = 0; k < names.length; k++) {
82
- lines.push(' ' + names[k] + ': { params: ' + names[k] + 'Params; result: ' + names[k] + 'Result };');
83
- }
84
- lines.push('}');
85
- lines.push('');
86
- return lines.join('\n');
87
- }
88
-
89
- function fetchSchema(url) {
90
- var endpoint = url.replace(/\/+$/, '');
91
- if (endpoint.indexOf('/rpc') !== endpoint.length - 4) {
92
- endpoint += '/rpc';
93
- }
94
- if (typeof fetch !== 'undefined') {
95
- return fetch(endpoint).then(function(res) {
96
- if (!res.ok) throw new Error('HTTP ' + res.status);
97
- return res.json();
98
- });
99
- }
100
- var mod = url.indexOf('https') === 0 ? require('https') : require('http');
101
- return new Promise(function(resolve, reject) {
102
- mod.get(endpoint, function(res) {
103
- var data = '';
104
- res.on('data', function(c) { data += c; });
105
- res.on('end', function() {
106
- if (res.statusCode !== 200) return reject(new Error('HTTP ' + res.statusCode));
107
- try { resolve(JSON.parse(data)); }
108
- catch (e) { reject(e); }
109
- });
110
- }).on('error', reject);
111
- });
112
- }
113
-
114
- function main() {
115
- if (fs.existsSync(getConfigPath())) {
116
- return Promise.resolve();
117
- }
118
-
119
- var isInteractive = process.stdin.isTTY;
120
- var isCI = process.env.CI;
121
-
122
- if (!isInteractive || isCI) {
123
- return Promise.resolve();
124
- }
125
-
126
- var rl = readline.createInterface({ input: process.stdin, output: process.stdout });
127
- return new Promise(function(resolve) {
128
- rl.question('How are types distributed to the client?\n 1) workspace (default) - server writes types directly to your project\n 2) server - client fetches types via HTTP\nEnter choice [1]: ', function(answer) {
129
- rl.close();
130
- resolve(answer.trim() === '2' ? 'server' : 'workspace');
131
- });
132
- }).then(function(distribution) {
133
- if (distribution === 'workspace') {
134
- var config = { distribution: 'workspace' };
135
- fs.writeFileSync(getConfigPath(), JSON.stringify(config, null, 2) + '\n');
136
- console.log(' \u2713 pyrpc-client.json created (workspace mode)');
137
- return;
138
- }
139
-
140
- var rl2 = readline.createInterface({ input: process.stdin, output: process.stdout });
141
- return new Promise(function(resolve) {
142
- rl2.question('pyRPC backend URL (default: http://localhost:8000): ', function(answer) {
143
- rl2.close();
144
- resolve(answer.trim() || 'http://localhost:8000');
145
- });
146
- }).then(function(url) {
147
- return fetchSchema(url).then(function(schemas) {
148
- var code = generate(schemas);
149
- fs.mkdirSync(path.dirname(TYPES_FILE), { recursive: true });
150
- fs.writeFileSync(TYPES_FILE, code, 'utf-8');
151
- console.log(' \u2713 @pyrpc/types: generated for ' + Object.keys(schemas).length + ' procedures');
152
-
153
- var config = { distribution: 'server', server_url: url };
154
- fs.writeFileSync(getConfigPath(), JSON.stringify(config, null, 2) + '\n');
155
- console.log(' \u2713 pyrpc-client.json created (server mode)');
156
- }).catch(function(err) {
157
- console.log(' \u2717 @pyrpc/types: could not connect (' + err.message + ')');
158
- console.log(' Run later: npx pyrpc sync');
159
-
160
- var config = { distribution: 'server', server_url: url };
161
- fs.writeFileSync(getConfigPath(), JSON.stringify(config, null, 2) + '\n');
162
- console.log(' \u2713 pyrpc-client.json created (server mode)');
163
- });
164
- });
165
- });
166
- }
167
-
168
- if (require.main === module) {
169
- main().catch(function(e) {});
170
- }
171
-
172
- module.exports = { main, toTs, generate, fetchSchema, findProjectRoot, getConfigPath, TYPES_FILE, PLACEHOLDER_MARKER };