b-gsdk 0.1.2 → 0.1.5

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/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
  },
@@ -44,19 +44,11 @@ async function main(args) {
44
44
  }, false);
45
45
  createDirIfDoesNotExist(`${bgsdkDirectoryPath}/generated`);
46
46
  fs_1.default.writeFileSync(path_1.default.join(bgsdkDirectoryPath, "generated/graphql.schema.json"), schemaCodegen.content);
47
- fs_1.default.writeFileSync(path_1.default.join(bgsdkDirectoryPath, "generated/index.ts"), sdkCodegen.content);
47
+ fs_1.default.writeFileSync(path_1.default.join(bgsdkDirectoryPath, "generated/index.ts"), "/* eslint-disable */\n" + sdkCodegen.content + "\n" + extraGenerated);
48
48
  const skdFilePath = path_1.default.join(bgsdkDirectoryPath, "sdk.ts");
49
49
  if (!fs_1.default.existsSync(skdFilePath)) {
50
50
  fs_1.default.writeFileSync(skdFilePath, sdkFileContents);
51
51
  }
52
- const gitignorePath = path_1.default.join(process.cwd(), ".gitignore");
53
- if (fs_1.default.existsSync(gitignorePath)) {
54
- const gitignore = fs_1.default.readFileSync(gitignorePath, "utf8");
55
- if (!gitignore.includes("generated")) {
56
- fs_1.default.appendFileSync(gitignorePath, "\ngenerated/");
57
- console.log('Added "generated/" to .gitignore');
58
- }
59
- }
60
52
  console.log("Done ✨");
61
53
  }
62
54
  exports.main = main;
@@ -65,15 +57,15 @@ function createDirIfDoesNotExist(p) {
65
57
  fs_1.default.mkdirSync(p);
66
58
  }
67
59
  }
68
- const sdkFileContents = `import { GraphQLClient } from 'graphql-request'
69
- import { getSdk } from './generated'
70
-
71
- export type CreateBGsdkClientParams = {
60
+ const extraGenerated = `export type CreateBgSdkParams = {
72
61
  endpoint: string
73
62
  headers?: string[][] | Record<string, string> | Headers
74
63
  }
75
64
 
76
- export const createBGsdk = ({ endpoint, headers }: CreateBGsdkClientParams) => {
65
+ export const createBgSdk = ({
66
+ endpoint,
67
+ headers
68
+ }: CreateBgSdkParams) => {
77
69
  const graphQLClient = new GraphQLClient(endpoint, {
78
70
  headers: {
79
71
  accept: 'application/json',
@@ -84,11 +76,12 @@ export const createBGsdk = ({ endpoint, headers }: CreateBGsdkClientParams) => {
84
76
 
85
77
  const generatedSdk = getSdk(graphQLClient)
86
78
 
87
- return { ...generatedSdk, rawClient: graphQLClient }
79
+ return { ...generatedSdk, client: graphQLClient }
88
80
  }
81
+ `;
82
+ const sdkFileContents = `import config from './config'
83
+ import { createBgSdk } from './generated'
89
84
 
90
- // You can then create the sdk with the endpoint and headers set up and export it.
91
- // For example like this:
92
- // export const bgsdk = createBGsdk({ })
85
+ export const bgSdk = createBgSdk(config)
93
86
 
94
87
  `;
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "b-gsdk",
3
- "version": "0.1.2",
3
+ "version": "0.1.5",
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
  },
@@ -57,22 +57,13 @@ export async function main(args: Args) {
57
57
  );
58
58
  fs.writeFileSync(
59
59
  path.join(bgsdkDirectoryPath, "generated/index.ts"),
60
- sdkCodegen.content
60
+ "/* eslint-disable */\n" + sdkCodegen.content + "\n" + extraGenerated
61
61
  );
62
62
  const skdFilePath = path.join(bgsdkDirectoryPath, "sdk.ts");
63
63
  if (!fs.existsSync(skdFilePath)) {
64
64
  fs.writeFileSync(skdFilePath, sdkFileContents);
65
65
  }
66
66
 
67
- const gitignorePath = path.join(process.cwd(), ".gitignore");
68
- if (fs.existsSync(gitignorePath)) {
69
- const gitignore = fs.readFileSync(gitignorePath, "utf8");
70
- if (!gitignore.includes("generated")) {
71
- fs.appendFileSync(gitignorePath, "\ngenerated/");
72
- console.log('Added "generated/" to .gitignore');
73
- }
74
- }
75
-
76
67
  console.log("Done ✨");
77
68
  }
78
69
 
@@ -82,15 +73,15 @@ function createDirIfDoesNotExist(p: string) {
82
73
  }
83
74
  }
84
75
 
85
- const sdkFileContents = `import { GraphQLClient } from 'graphql-request'
86
- import { getSdk } from './generated'
87
-
88
- export type CreateBGsdkClientParams = {
76
+ const extraGenerated = `export type CreateBgSdkParams = {
89
77
  endpoint: string
90
78
  headers?: string[][] | Record<string, string> | Headers
91
79
  }
92
80
 
93
- export const createBGsdk = ({ endpoint, headers }: CreateBGsdkClientParams) => {
81
+ export const createBgSdk = ({
82
+ endpoint,
83
+ headers
84
+ }: CreateBgSdkParams) => {
94
85
  const graphQLClient = new GraphQLClient(endpoint, {
95
86
  headers: {
96
87
  accept: 'application/json',
@@ -101,11 +92,13 @@ export const createBGsdk = ({ endpoint, headers }: CreateBGsdkClientParams) => {
101
92
 
102
93
  const generatedSdk = getSdk(graphQLClient)
103
94
 
104
- return { ...generatedSdk, rawClient: graphQLClient }
95
+ return { ...generatedSdk, client: graphQLClient }
105
96
  }
97
+ `;
98
+
99
+ const sdkFileContents = `import config from './config'
100
+ import { createBgSdk } from './generated'
106
101
 
107
- // You can then create the sdk with the endpoint and headers set up and export it.
108
- // For example like this:
109
- // export const bgsdk = createBGsdk({ })
102
+ export const bgSdk = createBgSdk(config)
110
103
 
111
104
  `;
@@ -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
  };
package/dist/bin.js DELETED
@@ -1,39 +0,0 @@
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
- const _1 = require(".");
8
- const format_error_1 = require("./util/format-error");
9
- const arg_1 = __importDefault(require("arg"));
10
- // Show usage and exit with code
11
- function help(code) {
12
- console.log(`Usage:
13
-
14
- b-gsdk generate
15
-
16
- `);
17
- process.exit(code);
18
- }
19
- // Get CLI arguments
20
- const [, , cmd] = process.argv;
21
- const args = (0, arg_1.default)({
22
- // types
23
- "--dir": String,
24
- // aliases
25
- "-d": "--dir",
26
- }, { permissive: true });
27
- // CLI commands
28
- const cmds = {
29
- generate: _1.main,
30
- };
31
- // Run CLI
32
- try {
33
- // Run command or show usage for unknown command
34
- cmds[cmd] ? cmds[cmd](args) : help(0);
35
- }
36
- catch (e) {
37
- console.error((0, format_error_1.formatError)(e).message);
38
- process.exit(1);
39
- }
@@ -1,16 +0,0 @@
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;
@@ -1,23 +0,0 @@
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
- schemaURL: zod_1.z.string(),
12
- headers: zod_1.z.record(zod_1.z.string()),
13
- });
14
- const getBGsdkConfig = (directoryPath) => {
15
- const bgsdkConfigPath = path_1.default.join(directoryPath, "config.json");
16
- if (!fs_1.default.existsSync(bgsdkConfigPath)) {
17
- throw new Error(`Could not find config.json in ${directoryPath}! Please create one.`);
18
- }
19
- const rawConfig = JSON.parse(fs_1.default.readFileSync(bgsdkConfigPath, "utf8"));
20
- const parsedConfig = configSchema.parse(rawConfig);
21
- return parsedConfig;
22
- };
23
- exports.getBGsdkConfig = getBGsdkConfig;
@@ -1,16 +0,0 @@
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;
@@ -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
- }