b-gsdk 0.0.4 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- package/.nvmrc +1 -0
- package/README.md +1 -0
- package/dist/index.js +47 -12
- package/package.json +9 -8
- package/src/index.ts +53 -13
- package/dist/bin.js +0 -27
- package/dist/util/format-error.js +0 -16
- package/dist/util/get-b-gsdk-directory-path.js +0 -17
package/.nvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
16
|
package/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# b-gsdk
|
package/dist/index.js
CHANGED
@@ -5,18 +5,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
6
|
exports.main = void 0;
|
7
7
|
const cli_1 = require("@graphql-codegen/cli");
|
8
|
-
const resolve_pkg_1 = __importDefault(require("resolve-pkg"));
|
9
8
|
const fs_1 = __importDefault(require("fs"));
|
10
9
|
const path_1 = __importDefault(require("path"));
|
11
10
|
const get_b_gsdk_directory_path_1 = require("./util/get-b-gsdk-directory-path");
|
12
11
|
async function main() {
|
13
|
-
console.log("starting main");
|
14
12
|
const bgsdkDirectoryPath = (0, get_b_gsdk_directory_path_1.getBGsdkDirectoryPath)(process.cwd());
|
15
|
-
console.log("got dir path:", bgsdkDirectoryPath);
|
16
13
|
if (!bgsdkDirectoryPath) {
|
17
14
|
throw new Error("Make sure you have a b-gsdk directory in the root of your project.");
|
18
15
|
}
|
19
|
-
console.log("attempting codegen");
|
20
16
|
const [schemaCodegen, sdkCodegen] = await (0, cli_1.generate)({
|
21
17
|
schema: {
|
22
18
|
"https://mr-beast-2.myshopify.com/api/2021-10/graphql": {
|
@@ -46,14 +42,53 @@ async function main() {
|
|
46
42
|
},
|
47
43
|
},
|
48
44
|
}, false);
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
45
|
+
createDirIfDoesNotExist(`${bgsdkDirectoryPath}/generated`);
|
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);
|
48
|
+
const skdFilePath = path_1.default.join(bgsdkDirectoryPath, "sdk.ts");
|
49
|
+
if (!fs_1.default.existsSync(skdFilePath)) {
|
50
|
+
fs_1.default.writeFileSync(skdFilePath, sdkFileContents);
|
54
51
|
}
|
55
|
-
|
56
|
-
fs_1.default.
|
57
|
-
|
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
|
+
console.log("Done ✨");
|
58
61
|
}
|
59
62
|
exports.main = main;
|
63
|
+
function createDirIfDoesNotExist(p) {
|
64
|
+
if (!fs_1.default.existsSync(p)) {
|
65
|
+
fs_1.default.mkdirSync(p);
|
66
|
+
}
|
67
|
+
}
|
68
|
+
const sdkFileContents = `import { GraphQLClient } from 'graphql-request'
|
69
|
+
import { getSdk } from './generated'
|
70
|
+
|
71
|
+
export type CreateBGsdkClientParams = {
|
72
|
+
endpoint: string
|
73
|
+
headers?: string[][] | Record<string, string> | Headers
|
74
|
+
}
|
75
|
+
|
76
|
+
export const createBGsdk = ({ endpoint, headers }: CreateBGsdkClientParams) => {
|
77
|
+
const graphQLClient = new GraphQLClient(endpoint, {
|
78
|
+
headers: {
|
79
|
+
accept: 'application/json',
|
80
|
+
'Content-Type': 'application/json',
|
81
|
+
...headers
|
82
|
+
}
|
83
|
+
})
|
84
|
+
|
85
|
+
const generatedSdk = getSdk(graphQLClient)
|
86
|
+
|
87
|
+
return { ...generatedSdk, rawClient: graphQLClient }
|
88
|
+
}
|
89
|
+
|
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({ })
|
93
|
+
|
94
|
+
`;
|
package/package.json
CHANGED
@@ -1,19 +1,21 @@
|
|
1
1
|
{
|
2
2
|
"name": "b-gsdk",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.7",
|
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
|
+
"scripts": {
|
10
|
+
"build": "tsc",
|
11
|
+
"prepublish": "yarn build"
|
12
|
+
},
|
9
13
|
"dependencies": {
|
10
14
|
"@graphql-codegen/cli": "^2.6.2",
|
11
|
-
"@graphql-codegen/core": "^2.5.1",
|
12
15
|
"@graphql-codegen/introspection": "^2.1.0",
|
13
16
|
"@graphql-codegen/typescript": "^2.4.1",
|
14
17
|
"@graphql-codegen/typescript-graphql-request": "^4.3.2",
|
15
|
-
"@graphql-codegen/typescript-operations": "^2.2.1"
|
16
|
-
"resolve-pkg": "^2.0.0"
|
18
|
+
"@graphql-codegen/typescript-operations": "^2.2.1"
|
17
19
|
},
|
18
20
|
"peerDependencies": {
|
19
21
|
"graphql": "*",
|
@@ -23,12 +25,11 @@
|
|
23
25
|
"devDependencies": {
|
24
26
|
"@tsconfig/node16": "^1.0.2",
|
25
27
|
"@types/node": "^17.0.24",
|
26
|
-
"esbuild": "^0.14.36",
|
27
28
|
"graphql": "^16.3.0",
|
28
29
|
"ts-node": "^10.7.0",
|
29
30
|
"typescript": "^4.6.3"
|
30
31
|
},
|
31
|
-
"
|
32
|
-
"
|
32
|
+
"engines": {
|
33
|
+
"node": ">=16.0.0"
|
33
34
|
}
|
34
|
-
}
|
35
|
+
}
|
package/src/index.ts
CHANGED
@@ -1,21 +1,17 @@
|
|
1
1
|
import { generate } from "@graphql-codegen/cli";
|
2
|
-
import resolvePkg from "resolve-pkg";
|
3
2
|
import fs from "fs";
|
4
3
|
import path from "path";
|
5
4
|
import { getBGsdkDirectoryPath } from "./util/get-b-gsdk-directory-path";
|
6
5
|
|
7
6
|
export async function main() {
|
8
|
-
console.log("starting main");
|
9
7
|
const bgsdkDirectoryPath = getBGsdkDirectoryPath(process.cwd());
|
10
8
|
|
11
|
-
console.log("got dir path:", bgsdkDirectoryPath);
|
12
9
|
if (!bgsdkDirectoryPath) {
|
13
10
|
throw new Error(
|
14
11
|
"Make sure you have a b-gsdk directory in the root of your project."
|
15
12
|
);
|
16
13
|
}
|
17
14
|
|
18
|
-
console.log("attempting codegen");
|
19
15
|
const [schemaCodegen, sdkCodegen] = await generate(
|
20
16
|
{
|
21
17
|
schema: {
|
@@ -49,19 +45,63 @@ export async function main() {
|
|
49
45
|
},
|
50
46
|
false
|
51
47
|
);
|
52
|
-
|
53
|
-
|
54
|
-
const resolved = resolvePkg("@b-gsdk/client");
|
55
|
-
if (!resolved) {
|
56
|
-
throw new Error("Please install @b-gsdk/client");
|
57
|
-
}
|
48
|
+
|
49
|
+
createDirIfDoesNotExist(`${bgsdkDirectoryPath}/generated`);
|
58
50
|
fs.writeFileSync(
|
59
|
-
path.join(
|
51
|
+
path.join(bgsdkDirectoryPath, "generated/graphql.schema.json"),
|
60
52
|
schemaCodegen.content
|
61
53
|
);
|
62
54
|
fs.writeFileSync(
|
63
|
-
path.join(
|
55
|
+
path.join(bgsdkDirectoryPath, "generated/index.ts"),
|
64
56
|
sdkCodegen.content
|
65
57
|
);
|
66
|
-
|
58
|
+
const skdFilePath = path.join(bgsdkDirectoryPath, "sdk.ts");
|
59
|
+
if (!fs.existsSync(skdFilePath)) {
|
60
|
+
fs.writeFileSync(skdFilePath, sdkFileContents);
|
61
|
+
}
|
62
|
+
|
63
|
+
const gitignorePath = path.join(process.cwd(), ".gitignore");
|
64
|
+
if (fs.existsSync(gitignorePath)) {
|
65
|
+
const gitignore = fs.readFileSync(gitignorePath, "utf8");
|
66
|
+
if (!gitignore.includes("generated")) {
|
67
|
+
fs.appendFileSync(gitignorePath, "\ngenerated/");
|
68
|
+
console.log('Added "generated/" to .gitignore');
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
console.log("Done ✨");
|
73
|
+
}
|
74
|
+
|
75
|
+
function createDirIfDoesNotExist(p: string) {
|
76
|
+
if (!fs.existsSync(p)) {
|
77
|
+
fs.mkdirSync(p);
|
78
|
+
}
|
67
79
|
}
|
80
|
+
|
81
|
+
const sdkFileContents = `import { GraphQLClient } from 'graphql-request'
|
82
|
+
import { getSdk } from './generated'
|
83
|
+
|
84
|
+
export type CreateBGsdkClientParams = {
|
85
|
+
endpoint: string
|
86
|
+
headers?: string[][] | Record<string, string> | Headers
|
87
|
+
}
|
88
|
+
|
89
|
+
export const createBGsdk = ({ endpoint, headers }: CreateBGsdkClientParams) => {
|
90
|
+
const graphQLClient = new GraphQLClient(endpoint, {
|
91
|
+
headers: {
|
92
|
+
accept: 'application/json',
|
93
|
+
'Content-Type': 'application/json',
|
94
|
+
...headers
|
95
|
+
}
|
96
|
+
})
|
97
|
+
|
98
|
+
const generatedSdk = getSdk(graphQLClient)
|
99
|
+
|
100
|
+
return { ...generatedSdk, rawClient: graphQLClient }
|
101
|
+
}
|
102
|
+
|
103
|
+
// You can then create the sdk with the endpoint and headers set up and export it.
|
104
|
+
// For example like this:
|
105
|
+
// export const bgsdk = createBGsdk({ })
|
106
|
+
|
107
|
+
`;
|
package/dist/bin.js
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
#!/usr/bin/env node
|
2
|
-
"use strict";
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
4
|
-
const _1 = require(".");
|
5
|
-
const format_error_1 = require("./util/format-error");
|
6
|
-
// Show usage and exit with code
|
7
|
-
function help(code) {
|
8
|
-
console.log(`Usage:
|
9
|
-
b-gsdk generate
|
10
|
-
`);
|
11
|
-
process.exit(code);
|
12
|
-
}
|
13
|
-
// Get CLI arguments
|
14
|
-
const [, , cmd] = process.argv;
|
15
|
-
// CLI commands
|
16
|
-
const cmds = {
|
17
|
-
generate: _1.main,
|
18
|
-
};
|
19
|
-
// Run CLI
|
20
|
-
try {
|
21
|
-
// Run command or show usage for unknown command
|
22
|
-
cmds[cmd] ? cmds[cmd]() : help(0);
|
23
|
-
}
|
24
|
-
catch (e) {
|
25
|
-
console.error((0, format_error_1.formatError)(e).message);
|
26
|
-
process.exit(1);
|
27
|
-
}
|
@@ -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,17 +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
|
-
// taken from prisma https://github.com/prisma/prisma/blob/8f6b7c7c99c1c720cf5bfed5d563423e71c1b84f/packages/sdk/src/cli/getSchema.ts#L46-L49
|
10
|
-
const getBGsdkDirectoryPath = (cwd) => {
|
11
|
-
const schemaPath = path_1.default.join(cwd, "b-gsdk");
|
12
|
-
if (fs_1.default.existsSync(schemaPath)) {
|
13
|
-
return schemaPath;
|
14
|
-
}
|
15
|
-
return null;
|
16
|
-
};
|
17
|
-
exports.getBGsdkDirectoryPath = getBGsdkDirectoryPath;
|