@baeta/plugin-pagination 0.0.2

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/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # @baeta/plugin-pagination
2
+
3
+ ## 0.0.2
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [[`9f937f4`](https://github.com/andreisergiu98/baeta/commit/9f937f47d3464a082680047414ee13a76cf6c056), [`9f937f4`](https://github.com/andreisergiu98/baeta/commit/9f937f47d3464a082680047414ee13a76cf6c056)]:
8
+ - @baeta/generator-sdk@0.0.5
package/dist/index.cjs ADDED
@@ -0,0 +1,86 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// index.ts
2
+ var _generatorsdk = require('@baeta/generator-sdk');
3
+ var _path = require('path');
4
+ function printFields(fields) {
5
+ return fields.map((field) => ` ${field}`).join("\n");
6
+ }
7
+ function printType(name, fields) {
8
+ return `type ${name} {
9
+ ${printFields(fields)}
10
+ }`;
11
+ }
12
+ function printTypes(types) {
13
+ return types.join("\n\n");
14
+ }
15
+ function printPageInfo(addFields = []) {
16
+ return printType("PageInfo", [
17
+ ...addFields,
18
+ "hasPreviousPage: Boolean!",
19
+ "hasNextPage: Boolean!"
20
+ ]);
21
+ }
22
+ function printExport(moduleDefinitionName) {
23
+ const parsed = _path.parse.call(void 0, moduleDefinitionName);
24
+ const importName = parsed.ext === ".ts" ? parsed.name : moduleDefinitionName;
25
+ return `import { getConnectionsModule } from "./${importName}";
26
+
27
+ export const connectionsModule = getConnectionsModule()`;
28
+ }
29
+ function printConnectionTypes(name, typeOptions, options) {
30
+ const {
31
+ cursorType = "String!",
32
+ nodeType = name,
33
+ connectionFields = [],
34
+ edgeFields = []
35
+ } = typeOptions;
36
+ const connection = printType(`${name}Connection`, [
37
+ ...connectionFields,
38
+ "pageInfo: PageInfo!",
39
+ `edges: [${name}Edge]`
40
+ ]);
41
+ const edge = printType(`${name}Edge`, [
42
+ ...edgeFields,
43
+ `cursor: ${cursorType}`,
44
+ `node: ${nodeType}${options.nullableNode === false ? "!" : ""}`
45
+ ]);
46
+ return [connection, edge];
47
+ }
48
+ function paginationPlugin(options) {
49
+ return _generatorsdk.createPluginV1.call(void 0, {
50
+ name: "pagination",
51
+ actionName: "pagination connections",
52
+ generate: async (ctx, next) => {
53
+ const types = [printPageInfo(options.pageInfoFields)];
54
+ for (const name in options.types) {
55
+ const typeOptions = options.types[name];
56
+ if (typeOptions === false) {
57
+ continue;
58
+ }
59
+ types.push(...printConnectionTypes(name, typeOptions === true ? {} : typeOptions, options));
60
+ }
61
+ const connectionModuleDir = _path.join.call(void 0,
62
+ ctx.generatorOptions.modulesDir,
63
+ _nullishCoalesce(options.moduleName, () => ( "connections"))
64
+ );
65
+ const definitionFile = new (0, _generatorsdk.File)(
66
+ `${connectionModuleDir}/connections.gql`,
67
+ printTypes(types),
68
+ "pagination"
69
+ );
70
+ await definitionFile.write();
71
+ ctx.fileManager.add(definitionFile);
72
+ if (options.createExport !== false) {
73
+ const exportFile = new (0, _generatorsdk.File)(
74
+ `${connectionModuleDir}/index.ts`,
75
+ printExport(ctx.generatorOptions.moduleDefinitionName),
76
+ "pagination"
77
+ );
78
+ ctx.fileManager.add(exportFile);
79
+ }
80
+ return next();
81
+ }
82
+ });
83
+ }
84
+
85
+
86
+ exports.paginationPlugin = paginationPlugin;
@@ -0,0 +1,18 @@
1
+ import * as _baeta_generator_sdk from '@baeta/generator-sdk';
2
+
3
+ interface TypeOptions {
4
+ nodeType?: string;
5
+ cursorType?: string;
6
+ edgeFields?: string[];
7
+ connectionFields?: string[];
8
+ }
9
+ interface PaginationOptions {
10
+ types: Record<string, boolean | TypeOptions>;
11
+ nullableNode?: boolean;
12
+ pageInfoFields?: string[];
13
+ moduleName?: string;
14
+ createExport?: boolean;
15
+ }
16
+ declare function paginationPlugin(options: PaginationOptions): _baeta_generator_sdk.GeneratorPluginV1<{}>;
17
+
18
+ export { paginationPlugin };
package/dist/index.js ADDED
@@ -0,0 +1,86 @@
1
+ // index.ts
2
+ import { createPluginV1, File } from "@baeta/generator-sdk";
3
+ import { join, parse } from "path";
4
+ function printFields(fields) {
5
+ return fields.map((field) => ` ${field}`).join("\n");
6
+ }
7
+ function printType(name, fields) {
8
+ return `type ${name} {
9
+ ${printFields(fields)}
10
+ }`;
11
+ }
12
+ function printTypes(types) {
13
+ return types.join("\n\n");
14
+ }
15
+ function printPageInfo(addFields = []) {
16
+ return printType("PageInfo", [
17
+ ...addFields,
18
+ "hasPreviousPage: Boolean!",
19
+ "hasNextPage: Boolean!"
20
+ ]);
21
+ }
22
+ function printExport(moduleDefinitionName) {
23
+ const parsed = parse(moduleDefinitionName);
24
+ const importName = parsed.ext === ".ts" ? parsed.name : moduleDefinitionName;
25
+ return `import { getConnectionsModule } from "./${importName}";
26
+
27
+ export const connectionsModule = getConnectionsModule()`;
28
+ }
29
+ function printConnectionTypes(name, typeOptions, options) {
30
+ const {
31
+ cursorType = "String!",
32
+ nodeType = name,
33
+ connectionFields = [],
34
+ edgeFields = []
35
+ } = typeOptions;
36
+ const connection = printType(`${name}Connection`, [
37
+ ...connectionFields,
38
+ "pageInfo: PageInfo!",
39
+ `edges: [${name}Edge]`
40
+ ]);
41
+ const edge = printType(`${name}Edge`, [
42
+ ...edgeFields,
43
+ `cursor: ${cursorType}`,
44
+ `node: ${nodeType}${options.nullableNode === false ? "!" : ""}`
45
+ ]);
46
+ return [connection, edge];
47
+ }
48
+ function paginationPlugin(options) {
49
+ return createPluginV1({
50
+ name: "pagination",
51
+ actionName: "pagination connections",
52
+ generate: async (ctx, next) => {
53
+ const types = [printPageInfo(options.pageInfoFields)];
54
+ for (const name in options.types) {
55
+ const typeOptions = options.types[name];
56
+ if (typeOptions === false) {
57
+ continue;
58
+ }
59
+ types.push(...printConnectionTypes(name, typeOptions === true ? {} : typeOptions, options));
60
+ }
61
+ const connectionModuleDir = join(
62
+ ctx.generatorOptions.modulesDir,
63
+ options.moduleName ?? "connections"
64
+ );
65
+ const definitionFile = new File(
66
+ `${connectionModuleDir}/connections.gql`,
67
+ printTypes(types),
68
+ "pagination"
69
+ );
70
+ await definitionFile.write();
71
+ ctx.fileManager.add(definitionFile);
72
+ if (options.createExport !== false) {
73
+ const exportFile = new File(
74
+ `${connectionModuleDir}/index.ts`,
75
+ printExport(ctx.generatorOptions.moduleDefinitionName),
76
+ "pagination"
77
+ );
78
+ ctx.fileManager.add(exportFile);
79
+ }
80
+ return next();
81
+ }
82
+ });
83
+ }
84
+ export {
85
+ paginationPlugin
86
+ };
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@baeta/plugin-pagination",
3
+ "version": "0.0.2",
4
+ "homepage": "https://github.com/andreisergiu98/baeta#readme",
5
+ "bugs": {
6
+ "url": "https://github.com/andreisergiu98/baeta/issues"
7
+ },
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/andreisergiu98/baeta.git",
11
+ "directory": "plugins/pagination"
12
+ },
13
+ "license": "MIT",
14
+ "author": "Andrei Pampu <pampu.andrei@pm.me>",
15
+ "type": "module",
16
+ "files": [
17
+ "dist",
18
+ "package.json"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsup",
22
+ "prepack": "prep",
23
+ "postpack": "prep --clean",
24
+ "types": "tsc --noEmit"
25
+ },
26
+ "dependencies": {
27
+ "@baeta/generator-sdk": "^0.0.5"
28
+ },
29
+ "devDependencies": {
30
+ "@baeta/build-tools": "^0.0.0",
31
+ "@baeta/tsconfig": "^0.0.0",
32
+ "@types/node": "^18.11.18",
33
+ "graphql": "^16.6.0",
34
+ "typescript": "^4.9.4"
35
+ },
36
+ "peerDependencies": {
37
+ "graphql": "^16.6.0"
38
+ },
39
+ "engines": {
40
+ "node": ">=18.0.0"
41
+ },
42
+ "publishConfig": {
43
+ "access": "public",
44
+ "exports": {
45
+ ".": {
46
+ "import": "./dist/index.js",
47
+ "require": "./dist/index.cjs",
48
+ "types": "./dist/index.d.ts"
49
+ }
50
+ },
51
+ "types": "dist/index.d.ts"
52
+ },
53
+ "exports": {
54
+ ".": {
55
+ "import": "./dist/index.js",
56
+ "require": "./dist/index.cjs",
57
+ "types": "./dist/index.d.ts"
58
+ }
59
+ },
60
+ "types": "dist/index.d.ts"
61
+ }