b-gsdk 0.0.4 → 0.0.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 +32 -13
- package/package.json +2 -4
- package/src/index.ts +35 -13
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,37 @@ async function main() {
|
|
46
42
|
},
|
47
43
|
},
|
48
44
|
}, false);
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
}
|
55
|
-
fs_1.default.writeFileSync(path_1.default.join(resolved, "generated/graphql.schema.json"), schemaCodegen.content);
|
56
|
-
fs_1.default.writeFileSync(path_1.default.join(resolved, "generated/index.ts"), sdkCodegen.content);
|
57
|
-
console.log("done");
|
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 ✨");
|
58
50
|
}
|
59
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
|
+
`;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
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",
|
@@ -12,8 +12,7 @@
|
|
12
12
|
"@graphql-codegen/introspection": "^2.1.0",
|
13
13
|
"@graphql-codegen/typescript": "^2.4.1",
|
14
14
|
"@graphql-codegen/typescript-graphql-request": "^4.3.2",
|
15
|
-
"@graphql-codegen/typescript-operations": "^2.2.1"
|
16
|
-
"resolve-pkg": "^2.0.0"
|
15
|
+
"@graphql-codegen/typescript-operations": "^2.2.1"
|
17
16
|
},
|
18
17
|
"peerDependencies": {
|
19
18
|
"graphql": "*",
|
@@ -23,7 +22,6 @@
|
|
23
22
|
"devDependencies": {
|
24
23
|
"@tsconfig/node16": "^1.0.2",
|
25
24
|
"@types/node": "^17.0.24",
|
26
|
-
"esbuild": "^0.14.36",
|
27
25
|
"graphql": "^16.3.0",
|
28
26
|
"ts-node": "^10.7.0",
|
29
27
|
"typescript": "^4.6.3"
|
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,45 @@ 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
|
+
fs.writeFileSync(path.join(bgsdkDirectoryPath, "sdk.ts"), sdkFileContents);
|
59
|
+
console.log("Done ✨");
|
60
|
+
}
|
61
|
+
|
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 }
|
67
88
|
}
|
89
|
+
`;
|