b-gsdk 0.1.0 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/bin.js +40 -0
- package/dist/index.js +1 -1
- package/dist/util/format-error.js +16 -0
- package/dist/util/get-b-gsdk-config.js +30 -0
- package/dist/util/get-b-gsdk-directory-path.js +16 -0
- package/package.json +3 -1
- package/src/bin.ts +1 -0
- package/src/index.d.ts +1 -0
- package/src/index.ts +1 -1
- package/src/util/get-b-gsdk-config.ts +14 -8
- package/src/docs/fragments/product.gql +0 -10
package/dist/bin.js
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
"use strict";
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
5
|
+
};
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
7
|
+
require("dotenv").config();
|
8
|
+
const _1 = require(".");
|
9
|
+
const format_error_1 = require("./util/format-error");
|
10
|
+
const arg_1 = __importDefault(require("arg"));
|
11
|
+
// Show usage and exit with code
|
12
|
+
function help(code) {
|
13
|
+
console.log(`Usage:
|
14
|
+
|
15
|
+
b-gsdk generate
|
16
|
+
|
17
|
+
`);
|
18
|
+
process.exit(code);
|
19
|
+
}
|
20
|
+
// Get CLI arguments
|
21
|
+
const [, , cmd] = process.argv;
|
22
|
+
const args = (0, arg_1.default)({
|
23
|
+
// types
|
24
|
+
"--dir": String,
|
25
|
+
// aliases
|
26
|
+
"-d": "--dir",
|
27
|
+
}, { permissive: true });
|
28
|
+
// CLI commands
|
29
|
+
const cmds = {
|
30
|
+
generate: _1.main,
|
31
|
+
};
|
32
|
+
// Run CLI
|
33
|
+
try {
|
34
|
+
// Run command or show usage for unknown command
|
35
|
+
cmds[cmd] ? cmds[cmd](args) : help(0);
|
36
|
+
}
|
37
|
+
catch (e) {
|
38
|
+
console.error((0, format_error_1.formatError)(e).message);
|
39
|
+
process.exit(1);
|
40
|
+
}
|
package/dist/index.js
CHANGED
@@ -17,7 +17,7 @@ async function main(args) {
|
|
17
17
|
const config = (0, get_b_gsdk_config_1.getBGsdkConfig)(bgsdkDirectoryPath);
|
18
18
|
const [schemaCodegen, sdkCodegen] = await (0, cli_1.generate)({
|
19
19
|
schema: {
|
20
|
-
[config.
|
20
|
+
[config.endpoint]: {
|
21
21
|
headers: config.headers,
|
22
22
|
},
|
23
23
|
},
|
@@ -0,0 +1,16 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.formatError = void 0;
|
4
|
+
const formatError = (error) => {
|
5
|
+
if (error instanceof Error) {
|
6
|
+
return error;
|
7
|
+
}
|
8
|
+
if (typeof error === "string") {
|
9
|
+
return new Error(error);
|
10
|
+
}
|
11
|
+
if (typeof error === "object") {
|
12
|
+
return new Error(JSON.stringify(error, null, 2));
|
13
|
+
}
|
14
|
+
return new Error(`Unknown error: ${error}`);
|
15
|
+
};
|
16
|
+
exports.formatError = formatError;
|
@@ -0,0 +1,30 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.getBGsdkConfig = void 0;
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
8
|
+
const path_1 = __importDefault(require("path"));
|
9
|
+
const zod_1 = require("zod");
|
10
|
+
const configSchema = zod_1.z.object({
|
11
|
+
endpoint: zod_1.z.string(),
|
12
|
+
headers: zod_1.z.record(zod_1.z.string()).optional(),
|
13
|
+
});
|
14
|
+
const getBGsdkConfig = (directoryPath) => {
|
15
|
+
const bgsdkConfigJsonPath = path_1.default.join(directoryPath, "config.json");
|
16
|
+
const bgsdkConfigJSPath = path_1.default.join(directoryPath, "config.js");
|
17
|
+
let rawConfig = {};
|
18
|
+
if (fs_1.default.existsSync(bgsdkConfigJsonPath)) {
|
19
|
+
rawConfig = JSON.parse(fs_1.default.readFileSync(bgsdkConfigJsonPath, "utf8"));
|
20
|
+
}
|
21
|
+
else if (fs_1.default.existsSync(bgsdkConfigJSPath)) {
|
22
|
+
rawConfig = require(bgsdkConfigJSPath);
|
23
|
+
}
|
24
|
+
else {
|
25
|
+
throw new Error(`Could not find config.{json,js} in ${directoryPath}`);
|
26
|
+
}
|
27
|
+
const parsedConfig = configSchema.parse(rawConfig);
|
28
|
+
return parsedConfig;
|
29
|
+
};
|
30
|
+
exports.getBGsdkConfig = getBGsdkConfig;
|
@@ -0,0 +1,16 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.getBGsdkDirectoryPath = void 0;
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
8
|
+
const path_1 = __importDefault(require("path"));
|
9
|
+
const getBGsdkDirectoryPath = (cwd, customDir) => {
|
10
|
+
const schemaPath = path_1.default.join(cwd, customDir || "b-gsdk");
|
11
|
+
if (fs_1.default.existsSync(schemaPath)) {
|
12
|
+
return schemaPath;
|
13
|
+
}
|
14
|
+
return null;
|
15
|
+
};
|
16
|
+
exports.getBGsdkDirectoryPath = getBGsdkDirectoryPath;
|
package/package.json
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
{
|
2
2
|
"name": "b-gsdk",
|
3
|
-
"version": "0.1.
|
3
|
+
"version": "0.1.3",
|
4
4
|
"description": "A GraphQL Codegen that outputs a TypeScript SDK.",
|
5
5
|
"author": "Julian Benegas",
|
6
6
|
"license": "MIT",
|
7
7
|
"main": "dist/index.js",
|
8
8
|
"bin": "dist/bin.js",
|
9
|
+
"types": "src/index.d.ts",
|
9
10
|
"scripts": {
|
10
11
|
"build": "tsc",
|
11
12
|
"prepublish": "yarn build"
|
@@ -17,6 +18,7 @@
|
|
17
18
|
"@graphql-codegen/typescript-graphql-request": "^4.3.2",
|
18
19
|
"@graphql-codegen/typescript-operations": "^2.2.1",
|
19
20
|
"arg": "^5.0.1",
|
21
|
+
"dotenv": "^16.0.0",
|
20
22
|
"zod": "^3.14.4"
|
21
23
|
},
|
22
24
|
"peerDependencies": {
|
package/src/bin.ts
CHANGED
package/src/index.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export { BGsdkConfig } from "./util/get-b-gsdk-config";
|
package/src/index.ts
CHANGED
@@ -3,18 +3,24 @@ import path from "path";
|
|
3
3
|
import { z } from "zod";
|
4
4
|
|
5
5
|
const configSchema = z.object({
|
6
|
-
|
7
|
-
headers: z.record(z.string()),
|
6
|
+
endpoint: z.string(),
|
7
|
+
headers: z.record(z.string()).optional(),
|
8
8
|
});
|
9
9
|
|
10
|
+
export type BGsdkConfig = z.infer<typeof configSchema>;
|
11
|
+
|
10
12
|
export const getBGsdkConfig = (directoryPath: string) => {
|
11
|
-
const
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
const bgsdkConfigJsonPath = path.join(directoryPath, "config.json");
|
14
|
+
const bgsdkConfigJSPath = path.join(directoryPath, "config.js");
|
15
|
+
|
16
|
+
let rawConfig = {};
|
17
|
+
if (fs.existsSync(bgsdkConfigJsonPath)) {
|
18
|
+
rawConfig = JSON.parse(fs.readFileSync(bgsdkConfigJsonPath, "utf8"));
|
19
|
+
} else if (fs.existsSync(bgsdkConfigJSPath)) {
|
20
|
+
rawConfig = require(bgsdkConfigJSPath);
|
21
|
+
} else {
|
22
|
+
throw new Error(`Could not find config.{json,js} in ${directoryPath}`);
|
16
23
|
}
|
17
|
-
const rawConfig = JSON.parse(fs.readFileSync(bgsdkConfigPath, "utf8"));
|
18
24
|
const parsedConfig = configSchema.parse(rawConfig);
|
19
25
|
return parsedConfig;
|
20
26
|
};
|