b-gsdk 0.0.2 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/bin.js +27 -0
- package/dist/index.js +78 -0
- package/dist/util/format-error.js +16 -0
- package/dist/util/get-b-gsdk-directory-path.js +17 -0
- package/package.json +5 -10
- package/src/bin.ts +29 -0
- package/src/index.ts +46 -15
- package/src/util/format-error.ts +15 -0
- package/src/util/get-b-gsdk-directory-path.ts +12 -0
- package/tsconfig.json +2 -5
- package/bin/fsevents.node +0 -0
- package/bin/index.js +0 -165
- package/bin/worker.js +0 -35
package/dist/bin.js
ADDED
@@ -0,0 +1,27 @@
|
|
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
|
+
}
|
package/dist/index.js
ADDED
@@ -0,0 +1,78 @@
|
|
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.main = void 0;
|
7
|
+
const cli_1 = require("@graphql-codegen/cli");
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
9
|
+
const path_1 = __importDefault(require("path"));
|
10
|
+
const get_b_gsdk_directory_path_1 = require("./util/get-b-gsdk-directory-path");
|
11
|
+
async function main() {
|
12
|
+
const bgsdkDirectoryPath = (0, get_b_gsdk_directory_path_1.getBGsdkDirectoryPath)(process.cwd());
|
13
|
+
if (!bgsdkDirectoryPath) {
|
14
|
+
throw new Error("Make sure you have a b-gsdk directory in the root of your project.");
|
15
|
+
}
|
16
|
+
const [schemaCodegen, sdkCodegen] = await (0, cli_1.generate)({
|
17
|
+
schema: {
|
18
|
+
"https://mr-beast-2.myshopify.com/api/2021-10/graphql": {
|
19
|
+
headers: {
|
20
|
+
"x-shopify-storefront-access-token": "374a3639228aeb7798d99d88002c4b2e",
|
21
|
+
},
|
22
|
+
},
|
23
|
+
},
|
24
|
+
generates: {
|
25
|
+
[__dirname + "/generated/index.ts"]: {
|
26
|
+
documents: [
|
27
|
+
bgsdkDirectoryPath + "/**/*.{gql,graphql}",
|
28
|
+
bgsdkDirectoryPath + "/*.{gql,graphql}",
|
29
|
+
],
|
30
|
+
plugins: [
|
31
|
+
"typescript",
|
32
|
+
"typescript-operations",
|
33
|
+
"typescript-graphql-request",
|
34
|
+
],
|
35
|
+
},
|
36
|
+
[__dirname + "/generated/graphql.schema.json"]: {
|
37
|
+
plugins: ["introspection"],
|
38
|
+
config: {
|
39
|
+
documentMode: "documentNode",
|
40
|
+
withHooks: true,
|
41
|
+
},
|
42
|
+
},
|
43
|
+
},
|
44
|
+
}, false);
|
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
|
+
fs_1.default.writeFileSync(path_1.default.join(bgsdkDirectoryPath, "sdk.ts"), sdkFileContents);
|
49
|
+
console.log("Done ✨");
|
50
|
+
}
|
51
|
+
exports.main = main;
|
52
|
+
function createDirIfDoesNotExist(p) {
|
53
|
+
if (!fs_1.default.existsSync(p)) {
|
54
|
+
fs_1.default.mkdirSync(p);
|
55
|
+
}
|
56
|
+
}
|
57
|
+
const sdkFileContents = `import { GraphQLClient } from 'graphql-request'
|
58
|
+
import { getSdk } from './generated'
|
59
|
+
|
60
|
+
export type CreateBGsdkClientParams = {
|
61
|
+
endpoint: string
|
62
|
+
headers?: string[][] | Record<string, string> | Headers
|
63
|
+
}
|
64
|
+
|
65
|
+
export const createBGsdk = ({ endpoint, headers }: CreateBGsdkClientParams) => {
|
66
|
+
const graphQLClient = new GraphQLClient(endpoint, {
|
67
|
+
headers: {
|
68
|
+
accept: 'application/json',
|
69
|
+
'Content-Type': 'application/json',
|
70
|
+
...headers
|
71
|
+
}
|
72
|
+
})
|
73
|
+
|
74
|
+
const generatedSdk = getSdk(graphQLClient)
|
75
|
+
|
76
|
+
return { ...generatedSdk, rawClient: graphQLClient }
|
77
|
+
}
|
78
|
+
`;
|
@@ -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,17 @@
|
|
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;
|
package/package.json
CHANGED
@@ -1,21 +1,18 @@
|
|
1
1
|
{
|
2
2
|
"name": "b-gsdk",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.5",
|
4
4
|
"description": "A GraphQL Codegen that outputs a TypeScript SDK.",
|
5
5
|
"author": "Julian Benegas",
|
6
6
|
"license": "MIT",
|
7
|
-
"main": "
|
8
|
-
"bin":
|
9
|
-
"b-gsdk": "./bin/index.js"
|
10
|
-
},
|
7
|
+
"main": "dist/index.js",
|
8
|
+
"bin": "dist/bin.js",
|
11
9
|
"dependencies": {
|
12
10
|
"@graphql-codegen/cli": "^2.6.2",
|
13
11
|
"@graphql-codegen/core": "^2.5.1",
|
14
12
|
"@graphql-codegen/introspection": "^2.1.0",
|
15
13
|
"@graphql-codegen/typescript": "^2.4.1",
|
16
14
|
"@graphql-codegen/typescript-graphql-request": "^4.3.2",
|
17
|
-
"@graphql-codegen/typescript-operations": "^2.2.1"
|
18
|
-
"resolve-pkg": "^2.0.0"
|
15
|
+
"@graphql-codegen/typescript-operations": "^2.2.1"
|
19
16
|
},
|
20
17
|
"peerDependencies": {
|
21
18
|
"graphql": "*",
|
@@ -25,13 +22,11 @@
|
|
25
22
|
"devDependencies": {
|
26
23
|
"@tsconfig/node16": "^1.0.2",
|
27
24
|
"@types/node": "^17.0.24",
|
28
|
-
"esbuild": "^0.14.36",
|
29
25
|
"graphql": "^16.3.0",
|
30
26
|
"ts-node": "^10.7.0",
|
31
27
|
"typescript": "^4.6.3"
|
32
28
|
},
|
33
29
|
"scripts": {
|
34
|
-
"build": "
|
35
|
-
"generate": "ts-node ./src/index.ts"
|
30
|
+
"build": "tsc"
|
36
31
|
}
|
37
32
|
}
|
package/src/bin.ts
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
|
3
|
+
import { main } from ".";
|
4
|
+
import { formatError } from "./util/format-error";
|
5
|
+
|
6
|
+
// Show usage and exit with code
|
7
|
+
function help(code: number) {
|
8
|
+
console.log(`Usage:
|
9
|
+
b-gsdk generate
|
10
|
+
`);
|
11
|
+
process.exit(code);
|
12
|
+
}
|
13
|
+
|
14
|
+
// Get CLI arguments
|
15
|
+
const [, , cmd] = process.argv;
|
16
|
+
|
17
|
+
// CLI commands
|
18
|
+
const cmds: { [key: string]: () => void } = {
|
19
|
+
generate: main,
|
20
|
+
};
|
21
|
+
|
22
|
+
// Run CLI
|
23
|
+
try {
|
24
|
+
// Run command or show usage for unknown command
|
25
|
+
cmds[cmd] ? cmds[cmd]() : help(0);
|
26
|
+
} catch (e) {
|
27
|
+
console.error(formatError(e).message);
|
28
|
+
process.exit(1);
|
29
|
+
}
|
package/src/index.ts
CHANGED
@@ -1,11 +1,17 @@
|
|
1
|
-
#!/usr/bin/env node
|
2
|
-
|
3
1
|
import { generate } from "@graphql-codegen/cli";
|
4
|
-
import resolvePkg from "resolve-pkg";
|
5
2
|
import fs from "fs";
|
6
3
|
import path from "path";
|
4
|
+
import { getBGsdkDirectoryPath } from "./util/get-b-gsdk-directory-path";
|
5
|
+
|
6
|
+
export async function main() {
|
7
|
+
const bgsdkDirectoryPath = getBGsdkDirectoryPath(process.cwd());
|
8
|
+
|
9
|
+
if (!bgsdkDirectoryPath) {
|
10
|
+
throw new Error(
|
11
|
+
"Make sure you have a b-gsdk directory in the root of your project."
|
12
|
+
);
|
13
|
+
}
|
7
14
|
|
8
|
-
async function main() {
|
9
15
|
const [schemaCodegen, sdkCodegen] = await generate(
|
10
16
|
{
|
11
17
|
schema: {
|
@@ -19,8 +25,8 @@ async function main() {
|
|
19
25
|
generates: {
|
20
26
|
[__dirname + "/generated/index.ts"]: {
|
21
27
|
documents: [
|
22
|
-
|
23
|
-
|
28
|
+
bgsdkDirectoryPath + "/**/*.{gql,graphql}",
|
29
|
+
bgsdkDirectoryPath + "/*.{gql,graphql}",
|
24
30
|
],
|
25
31
|
plugins: [
|
26
32
|
"typescript",
|
@@ -39,20 +45,45 @@ async function main() {
|
|
39
45
|
},
|
40
46
|
false
|
41
47
|
);
|
42
|
-
|
43
|
-
|
44
|
-
console.error("Please install @b-gsdk/client");
|
45
|
-
return;
|
46
|
-
}
|
48
|
+
|
49
|
+
createDirIfDoesNotExist(`${bgsdkDirectoryPath}/generated`);
|
47
50
|
fs.writeFileSync(
|
48
|
-
path.join(
|
51
|
+
path.join(bgsdkDirectoryPath, "generated/graphql.schema.json"),
|
49
52
|
schemaCodegen.content
|
50
53
|
);
|
51
54
|
fs.writeFileSync(
|
52
|
-
path.join(
|
55
|
+
path.join(bgsdkDirectoryPath, "generated/index.ts"),
|
53
56
|
sdkCodegen.content
|
54
57
|
);
|
55
|
-
|
58
|
+
fs.writeFileSync(path.join(bgsdkDirectoryPath, "sdk.ts"), sdkFileContents);
|
59
|
+
console.log("Done ✨");
|
56
60
|
}
|
57
61
|
|
58
|
-
|
62
|
+
function createDirIfDoesNotExist(p: string) {
|
63
|
+
if (!fs.existsSync(p)) {
|
64
|
+
fs.mkdirSync(p);
|
65
|
+
}
|
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
|
+
`;
|
@@ -0,0 +1,15 @@
|
|
1
|
+
export const formatError = (error: unknown): Error => {
|
2
|
+
if (error instanceof Error) {
|
3
|
+
return error;
|
4
|
+
}
|
5
|
+
|
6
|
+
if (typeof error === "string") {
|
7
|
+
return new Error(error);
|
8
|
+
}
|
9
|
+
|
10
|
+
if (typeof error === "object") {
|
11
|
+
return new Error(JSON.stringify(error, null, 2));
|
12
|
+
}
|
13
|
+
|
14
|
+
return new Error(`Unknown error: ${error}`);
|
15
|
+
};
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import fs from "fs";
|
2
|
+
import path from "path";
|
3
|
+
|
4
|
+
// taken from prisma https://github.com/prisma/prisma/blob/8f6b7c7c99c1c720cf5bfed5d563423e71c1b84f/packages/sdk/src/cli/getSchema.ts#L46-L49
|
5
|
+
export const getBGsdkDirectoryPath = (cwd: string) => {
|
6
|
+
const schemaPath = path.join(cwd, "b-gsdk");
|
7
|
+
if (fs.existsSync(schemaPath)) {
|
8
|
+
return schemaPath;
|
9
|
+
}
|
10
|
+
|
11
|
+
return null;
|
12
|
+
};
|
package/tsconfig.json
CHANGED
@@ -1,14 +1,10 @@
|
|
1
1
|
{
|
2
2
|
"extends": "@tsconfig/node16/tsconfig.json",
|
3
|
-
|
4
|
-
// Most ts-node options can be specified here using their programmatic names.
|
5
3
|
"ts-node": {
|
6
4
|
// It is faster to skip typechecking.
|
7
5
|
// Remove if you want ts-node to do typechecking.
|
8
6
|
"transpileOnly": true,
|
9
|
-
|
10
7
|
"files": true,
|
11
|
-
|
12
8
|
"compilerOptions": {
|
13
9
|
// compilerOptions specified here will override those declared below,
|
14
10
|
// but *only* in ts-node. Useful if you want ts-node and tsc to use
|
@@ -16,6 +12,7 @@
|
|
16
12
|
}
|
17
13
|
},
|
18
14
|
"compilerOptions": {
|
19
|
-
|
15
|
+
"rootDir": "src",
|
16
|
+
"outDir": "dist"
|
20
17
|
}
|
21
18
|
}
|
package/bin/fsevents.node
DELETED
Binary file
|