b-gsdk 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -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,6 +1,6 @@
1
1
  {
2
2
  "name": "b-gsdk",
3
- "version": "0.1.4",
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",
package/src/index.ts CHANGED
@@ -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
  `;
package/dist/bin.js DELETED
@@ -1,49 +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
- 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
- }
@@ -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,30 +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
- 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;
@@ -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;