b-gsdk 0.1.1 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
package/dist/bin.js ADDED
@@ -0,0 +1,49 @@
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]
36
+ ? cmds[cmd](args)
37
+ .then(() => {
38
+ process.exit(0);
39
+ })
40
+ .catch((error) => {
41
+ console.error((0, format_error_1.formatError)(error));
42
+ process.exit(1);
43
+ })
44
+ : help(0);
45
+ }
46
+ catch (e) {
47
+ console.error((0, format_error_1.formatError)(e).message);
48
+ process.exit(1);
49
+ }
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.schemaURL]: {
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.1",
3
+ "version": "0.1.4",
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": {
@@ -30,8 +32,5 @@
30
32
  "graphql": "^16.3.0",
31
33
  "ts-node": "^10.7.0",
32
34
  "typescript": "^4.6.3"
33
- },
34
- "engines": {
35
- "node": ">=16.0.0"
36
35
  }
37
36
  }
package/src/bin.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
+ require("dotenv").config();
3
4
  import { main } from ".";
4
5
  import { formatError } from "./util/format-error";
5
6
  import arg from "arg";
@@ -28,14 +29,23 @@ const args = arg(
28
29
  );
29
30
 
30
31
  // CLI commands
31
- const cmds: { [key: string]: (args: Args) => void } = {
32
+ const cmds: { [key: string]: (args: Args) => Promise<void> } = {
32
33
  generate: main,
33
34
  };
34
35
 
35
36
  // Run CLI
36
37
  try {
37
38
  // Run command or show usage for unknown command
38
- cmds[cmd] ? cmds[cmd](args) : help(0);
39
+ cmds[cmd]
40
+ ? cmds[cmd](args)
41
+ .then(() => {
42
+ process.exit(0);
43
+ })
44
+ .catch((error) => {
45
+ console.error(formatError(error));
46
+ process.exit(1);
47
+ })
48
+ : help(0);
39
49
  } catch (e) {
40
50
  console.error(formatError(e).message);
41
51
  process.exit(1);
package/src/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { BGsdkConfig } from "./util/get-b-gsdk-config";
package/src/index.ts CHANGED
@@ -22,7 +22,7 @@ export async function main(args: Args) {
22
22
  const [schemaCodegen, sdkCodegen] = await generate(
23
23
  {
24
24
  schema: {
25
- [config.schemaURL]: {
25
+ [config.endpoint]: {
26
26
  headers: config.headers,
27
27
  },
28
28
  },
@@ -3,18 +3,24 @@ import path from "path";
3
3
  import { z } from "zod";
4
4
 
5
5
  const configSchema = z.object({
6
- schemaURL: z.string(),
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 bgsdkConfigPath = path.join(directoryPath, "config.json");
12
- if (!fs.existsSync(bgsdkConfigPath)) {
13
- throw new Error(
14
- `Could not find config.json in ${directoryPath}! Please create one.`
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
  };
@@ -1,10 +0,0 @@
1
- fragment Cart on Cart {
2
- id
3
- checkoutUrl
4
- }
5
-
6
- query FetchCart($id: ID!) {
7
- cart(id: $id) {
8
- ...Cart
9
- }
10
- }