@takeshape/ssg 11.143.2 → 11.154.1
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/package.json +5 -5
- package/dist/compress-html.d.ts +0 -3
- package/dist/compress-html.js +0 -20
- package/dist/config.d.ts +0 -76
- package/dist/config.js +0 -106
- package/dist/errors/formatting.d.ts +0 -4
- package/dist/errors/formatting.js +0 -8
- package/dist/errors/graphql-error.d.ts +0 -6
- package/dist/errors/graphql-error.js +0 -22
- package/dist/errors/index.d.ts +0 -5
- package/dist/errors/index.js +0 -5
- package/dist/errors/pagination-error.d.ts +0 -3
- package/dist/errors/pagination-error.js +0 -6
- package/dist/errors/template-render-error.d.ts +0 -4
- package/dist/errors/template-render-error.js +0 -11
- package/dist/files.d.ts +0 -3
- package/dist/files.js +0 -11
- package/dist/filters/array-filters.d.ts +0 -2
- package/dist/filters/array-filters.js +0 -2
- package/dist/filters/code-filter.d.ts +0 -1
- package/dist/filters/code-filter.js +0 -13
- package/dist/filters/create-asset-filter.d.ts +0 -1
- package/dist/filters/create-asset-filter.js +0 -5
- package/dist/filters/create-date-filter.d.ts +0 -2
- package/dist/filters/create-date-filter.js +0 -12
- package/dist/filters/create-image-filter.d.ts +0 -2
- package/dist/filters/create-image-filter.js +0 -7
- package/dist/filters/create-number-filter.d.ts +0 -3
- package/dist/filters/create-number-filter.js +0 -120
- package/dist/filters/create-route-filter.d.ts +0 -3
- package/dist/filters/create-route-filter.js +0 -24
- package/dist/filters/markdown-filter.d.ts +0 -1
- package/dist/filters/markdown-filter.js +0 -6
- package/dist/filters/pluralize-filter.d.ts +0 -1
- package/dist/filters/pluralize-filter.js +0 -4
- package/dist/generate/context.d.ts +0 -8
- package/dist/generate/context.js +0 -49
- package/dist/generate/generate.d.ts +0 -21
- package/dist/generate/generate.js +0 -300
- package/dist/generate/index.d.ts +0 -1
- package/dist/generate/index.js +0 -1
- package/dist/generate/streams.d.ts +0 -7
- package/dist/generate/streams.js +0 -88
- package/dist/generate/types.d.ts +0 -54
- package/dist/generate/types.js +0 -1
- package/dist/graphql/analyze.d.ts +0 -28
- package/dist/graphql/analyze.js +0 -87
- package/dist/graphql/ast.d.ts +0 -19
- package/dist/graphql/ast.js +0 -122
- package/dist/graphql/client-schema.d.ts +0 -3
- package/dist/graphql/client-schema.js +0 -8
- package/dist/graphql/index.d.ts +0 -6
- package/dist/graphql/index.js +0 -6
- package/dist/graphql/migrate.d.ts +0 -19
- package/dist/graphql/migrate.js +0 -111
- package/dist/graphql/pagination.d.ts +0 -36
- package/dist/graphql/pagination.js +0 -268
- package/dist/graphql/query.d.ts +0 -20
- package/dist/graphql/query.js +0 -141
- package/dist/graphql/schema-connector-factory.d.ts +0 -10
- package/dist/graphql/schema-connector-factory.js +0 -90
- package/dist/gzip.d.ts +0 -3
- package/dist/gzip.js +0 -14
- package/dist/index.d.ts +0 -6
- package/dist/index.js +0 -6
- package/dist/nunjucks.d.ts +0 -7
- package/dist/nunjucks.js +0 -88
- package/dist/paths.d.ts +0 -2
- package/dist/paths.js +0 -10
- package/dist/resolve-context.d.ts +0 -34
- package/dist/resolve-context.js +0 -115
- package/dist/stats.d.ts +0 -4
- package/dist/stats.js +0 -39
- package/dist/types.d.ts +0 -53
- package/dist/types.js +0 -1
- package/dist/util.d.ts +0 -2
- package/dist/util.js +0 -17
package/dist/graphql/query.js
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { Kind, parse, print, TypeInfo, visit, visitWithTypeInfo } from 'graphql';
|
|
2
|
-
import { addQueryVariables, createArgument, createVariableDefinition, updateQuery, wrapQuery } from "./ast.js";
|
|
3
|
-
export function getItemType(fieldType) {
|
|
4
|
-
return 'ofType' in fieldType ? fieldType.ofType : fieldType;
|
|
5
|
-
}
|
|
6
|
-
export function getFields(type) {
|
|
7
|
-
return 'getFields' in type ? type.getFields() : undefined;
|
|
8
|
-
}
|
|
9
|
-
function addId(node) {
|
|
10
|
-
const selections = node.selectionSet && node.selectionSet.selections;
|
|
11
|
-
if (selections &&
|
|
12
|
-
!selections.find((selection) => (selection.kind === 'Field' || selection.kind === 'FragmentSpread') && selection.name.value === '_id')) {
|
|
13
|
-
selections.push({
|
|
14
|
-
kind: Kind.FIELD,
|
|
15
|
-
name: {
|
|
16
|
-
kind: Kind.NAME,
|
|
17
|
-
value: '_id'
|
|
18
|
-
},
|
|
19
|
-
arguments: [],
|
|
20
|
-
directives: []
|
|
21
|
-
});
|
|
22
|
-
return node;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
export function ensureIds(queryAst, schema) {
|
|
26
|
-
const typeInfo = new TypeInfo(schema);
|
|
27
|
-
const visitor = {
|
|
28
|
-
Field: {
|
|
29
|
-
enter(node) {
|
|
30
|
-
const fieldDef = typeInfo.getFieldDef();
|
|
31
|
-
if (fieldDef) {
|
|
32
|
-
const type = getItemType(fieldDef.type);
|
|
33
|
-
const fields = getFields(type);
|
|
34
|
-
if (fields?._id) {
|
|
35
|
-
return addId(node);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
return visit(queryAst, visitWithTypeInfo(typeInfo, visitor));
|
|
42
|
-
}
|
|
43
|
-
const addLocaleVariable = addQueryVariables.bind(null, [{ name: 'locale', type: 'String' }]);
|
|
44
|
-
const wrapWithContext = wrapQuery.bind(null, 'withContext', [{ name: 'locale', value: 'locale' }]);
|
|
45
|
-
export function wrapWithLocaleContext(ast) {
|
|
46
|
-
return wrapWithContext(addLocaleVariable(ast));
|
|
47
|
-
}
|
|
48
|
-
function unboxLocalizedResponse(res) {
|
|
49
|
-
return {
|
|
50
|
-
...res,
|
|
51
|
-
data: res.data && res.data.withContext
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
export const FILTER_VAR_NAME = '__filter';
|
|
55
|
-
export function addContentIdFilter(ast, schema) {
|
|
56
|
-
const typeInfo = new TypeInfo(schema);
|
|
57
|
-
let filterType;
|
|
58
|
-
const varsToRemove = new Set();
|
|
59
|
-
const visitor = {
|
|
60
|
-
Field: {
|
|
61
|
-
enter(node) {
|
|
62
|
-
const matches = /^get(.+)List$/.exec(node.name.value);
|
|
63
|
-
if (matches) {
|
|
64
|
-
const fieldDef = typeInfo.getFieldDef();
|
|
65
|
-
if (fieldDef) {
|
|
66
|
-
const filterArgType = fieldDef.args.find((arg) => arg.name === 'filter');
|
|
67
|
-
if (filterArgType) {
|
|
68
|
-
filterType = filterArgType.type.toString();
|
|
69
|
-
const filterArgAst = createArgument({
|
|
70
|
-
name: 'filter',
|
|
71
|
-
value: FILTER_VAR_NAME
|
|
72
|
-
});
|
|
73
|
-
return {
|
|
74
|
-
...node,
|
|
75
|
-
arguments: node.arguments
|
|
76
|
-
? node.arguments
|
|
77
|
-
.filter((arg) => {
|
|
78
|
-
if (arg.name.value === 'filter' && arg.value.kind === 'Variable') {
|
|
79
|
-
varsToRemove.add(arg.value.name.value);
|
|
80
|
-
return false;
|
|
81
|
-
}
|
|
82
|
-
return true;
|
|
83
|
-
})
|
|
84
|
-
.concat(filterArgAst)
|
|
85
|
-
: [filterArgAst]
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
let updatedAst = visit(ast, visitWithTypeInfo(typeInfo, visitor));
|
|
94
|
-
if (filterType) {
|
|
95
|
-
const type = filterType;
|
|
96
|
-
updatedAst = updateQuery(updatedAst, (query) => {
|
|
97
|
-
const variableDef = createVariableDefinition({
|
|
98
|
-
name: FILTER_VAR_NAME,
|
|
99
|
-
type
|
|
100
|
-
});
|
|
101
|
-
return {
|
|
102
|
-
...query,
|
|
103
|
-
variableDefinitions: query.variableDefinitions
|
|
104
|
-
? query.variableDefinitions
|
|
105
|
-
.filter((def) => !varsToRemove.has(def.variable.name.value))
|
|
106
|
-
.concat(variableDef)
|
|
107
|
-
: [variableDef]
|
|
108
|
-
};
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
return {
|
|
112
|
-
ast: updatedAst,
|
|
113
|
-
varName: FILTER_VAR_NAME,
|
|
114
|
-
modified: Boolean(filterType)
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
export function withQueryTransforms(connector, schema, { locale, usage, contentId } = {}) {
|
|
118
|
-
return async (gqlParams, context) => {
|
|
119
|
-
const variables = { ...gqlParams.variables };
|
|
120
|
-
let queryAst = parse(gqlParams.query);
|
|
121
|
-
if (usage) {
|
|
122
|
-
queryAst = ensureIds(queryAst, schema);
|
|
123
|
-
}
|
|
124
|
-
if (context?.isPaginated && contentId) {
|
|
125
|
-
const { ast, modified, varName } = addContentIdFilter(queryAst, schema);
|
|
126
|
-
if (modified) {
|
|
127
|
-
queryAst = ast;
|
|
128
|
-
variables[varName] = { term: { _id: contentId } };
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
if (locale) {
|
|
132
|
-
variables.locale ||= locale;
|
|
133
|
-
queryAst = wrapWithLocaleContext(queryAst);
|
|
134
|
-
}
|
|
135
|
-
const res = await connector({
|
|
136
|
-
query: print(queryAst),
|
|
137
|
-
variables
|
|
138
|
-
});
|
|
139
|
-
return locale ? unboxLocalizedResponse(res) : res;
|
|
140
|
-
};
|
|
141
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { GraphQLConnector } from '../types.ts';
|
|
2
|
-
type GraphQLConfig = {
|
|
3
|
-
type: string;
|
|
4
|
-
file?: string;
|
|
5
|
-
url?: string;
|
|
6
|
-
headers?: Record<string, string>;
|
|
7
|
-
};
|
|
8
|
-
export declare function getRemoteConnector(config: GraphQLConfig): GraphQLConnector;
|
|
9
|
-
export declare function schemaConnectorFactory(cwd: string, config?: GraphQLConfig): Promise<GraphQLConnector>;
|
|
10
|
-
export {};
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
-
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
-
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
-
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
-
});
|
|
6
|
-
}
|
|
7
|
-
return path;
|
|
8
|
-
};
|
|
9
|
-
import path from 'node:path';
|
|
10
|
-
import fse from 'fs-extra';
|
|
11
|
-
import { formatError, graphql } from 'graphql';
|
|
12
|
-
import fetch from 'node-fetch';
|
|
13
|
-
import { connectorWithMigrations } from "./migrate.js";
|
|
14
|
-
export function getRemoteConnector(config) {
|
|
15
|
-
const { url } = config;
|
|
16
|
-
if (!url) {
|
|
17
|
-
throw new Error('Invalid GraphQL config: missing url');
|
|
18
|
-
}
|
|
19
|
-
return async (params) => {
|
|
20
|
-
const res = await fetch(url, {
|
|
21
|
-
method: 'POST',
|
|
22
|
-
body: JSON.stringify(params),
|
|
23
|
-
headers: {
|
|
24
|
-
'Content-Type': 'application/json',
|
|
25
|
-
...(config.headers ?? {})
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
if (res.ok) {
|
|
29
|
-
return res.json();
|
|
30
|
-
}
|
|
31
|
-
const text = await res.text();
|
|
32
|
-
throw new Error(`${res.statusText}: ${text}`);
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
function parseConfigJson(json) {
|
|
36
|
-
if (json.request) {
|
|
37
|
-
return {
|
|
38
|
-
type: 'request',
|
|
39
|
-
...json.request
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
if (json['graphql-js']) {
|
|
43
|
-
return {
|
|
44
|
-
type: 'graphql-js',
|
|
45
|
-
file: json['graphql-js']
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
50
|
-
function readConfigFile(path) {
|
|
51
|
-
let json;
|
|
52
|
-
try {
|
|
53
|
-
json = JSON.parse(fse.readFileSync(`${path}/.graphqlrc`, 'utf-8'));
|
|
54
|
-
}
|
|
55
|
-
catch {
|
|
56
|
-
throw new Error('An error occurred loading .graphqlrc.');
|
|
57
|
-
}
|
|
58
|
-
const config = parseConfigJson(json);
|
|
59
|
-
if (config) {
|
|
60
|
-
return config;
|
|
61
|
-
}
|
|
62
|
-
throw new Error(`Invalid configuration file: ${JSON.stringify(json)}`);
|
|
63
|
-
}
|
|
64
|
-
export async function schemaConnectorFactory(cwd, config) {
|
|
65
|
-
config ??= readConfigFile(cwd);
|
|
66
|
-
let connector;
|
|
67
|
-
if (config.type === 'graphql-js' && config.file) {
|
|
68
|
-
const schema = await import(__rewriteRelativeImportExtension(path.join(cwd, config.file)));
|
|
69
|
-
connector = async (params) => {
|
|
70
|
-
const res = await graphql({
|
|
71
|
-
schema: schema.default || schema,
|
|
72
|
-
source: params.query,
|
|
73
|
-
rootValue: null,
|
|
74
|
-
contextValue: {},
|
|
75
|
-
variableValues: params.variables
|
|
76
|
-
});
|
|
77
|
-
return {
|
|
78
|
-
data: res.data,
|
|
79
|
-
errors: res.errors?.map(formatError)
|
|
80
|
-
};
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
if (config.type === 'request') {
|
|
84
|
-
connector = getRemoteConnector(config);
|
|
85
|
-
}
|
|
86
|
-
if (connector) {
|
|
87
|
-
return connectorWithMigrations(connector);
|
|
88
|
-
}
|
|
89
|
-
throw new Error('Could not create GraphQL connector - invalid configuration');
|
|
90
|
-
}
|
package/dist/gzip.d.ts
DELETED
package/dist/gzip.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { shouldCompress } from '@takeshape/util';
|
|
2
|
-
import { gzip } from '@takeshape/util/node';
|
|
3
|
-
export default function gzipContents(renderTemplate) {
|
|
4
|
-
return async (path, template, context) => {
|
|
5
|
-
const item = await renderTemplate(path, template, context);
|
|
6
|
-
if (shouldCompress(item.path)) {
|
|
7
|
-
return {
|
|
8
|
-
path: item.path,
|
|
9
|
-
contents: await gzip(item.contents)
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
return item;
|
|
13
|
-
};
|
|
14
|
-
}
|
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
package/dist/nunjucks.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { Config, SyncFileLoader } from './config.ts';
|
|
2
|
-
import TemplateRenderError from './errors/template-render-error.ts';
|
|
3
|
-
import type { RenderTemplate, Stats } from './types.ts';
|
|
4
|
-
export declare function formatError(error: Error): TemplateRenderError;
|
|
5
|
-
export type TemplateContext = Record<string, any>;
|
|
6
|
-
export type NunjucksRender = (templateName: string, context: TemplateContext) => Promise<string>;
|
|
7
|
-
export default function nunjucksFactory(fileLoader: SyncFileLoader, config: Config, stats: Stats): RenderTemplate;
|
package/dist/nunjucks.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import { deepClone } from '@takeshape/util';
|
|
3
|
-
import nunjucks from '@takeshape/vm-nunjucks';
|
|
4
|
-
import TemplateRenderError from "./errors/template-render-error.js";
|
|
5
|
-
import { intersectionFilter, unionFilter } from "./filters/array-filters.js";
|
|
6
|
-
import codeFilter from "./filters/code-filter.js";
|
|
7
|
-
import createAssetFilter from "./filters/create-asset-filter.js";
|
|
8
|
-
import createDateFilter from "./filters/create-date-filter.js";
|
|
9
|
-
import createImageFilter from "./filters/create-image-filter.js";
|
|
10
|
-
import createNumberFilter from "./filters/create-number-filter.js";
|
|
11
|
-
import createRouteFilter from "./filters/create-route-filter.js";
|
|
12
|
-
import markdownFilter from "./filters/markdown-filter.js";
|
|
13
|
-
import pluralizeFilter from "./filters/pluralize-filter.js";
|
|
14
|
-
import { permalink } from "./paths.js";
|
|
15
|
-
function parseLocation(line) {
|
|
16
|
-
const groups = /\(([^)]+)\)(?: \[Line (\d+)(?:, Column (\d+))?])?/.exec(line);
|
|
17
|
-
if (groups) {
|
|
18
|
-
return {
|
|
19
|
-
file: groups[1],
|
|
20
|
-
line: Number.parseInt(groups[2], 10),
|
|
21
|
-
column: Number.parseInt(groups[3], 10)
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
function stripMessage(message) {
|
|
26
|
-
return message.replace(/\s*(?:Error:)?\s*/, '');
|
|
27
|
-
}
|
|
28
|
-
export function formatError(error) {
|
|
29
|
-
const lines = error.message.split('\n');
|
|
30
|
-
const location = parseLocation(lines[0]);
|
|
31
|
-
const message = stripMessage(lines[lines.length - 1]);
|
|
32
|
-
return new TemplateRenderError(message, location);
|
|
33
|
-
}
|
|
34
|
-
class SyncLoader {
|
|
35
|
-
fileLoader;
|
|
36
|
-
constructor(fileLoader) {
|
|
37
|
-
this.async = false;
|
|
38
|
-
this.fileLoader = fileLoader;
|
|
39
|
-
}
|
|
40
|
-
getSource(name) {
|
|
41
|
-
const src = this.fileLoader(name);
|
|
42
|
-
return src === undefined ? undefined : { src, path: name, noCache: false };
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
// Manually promisify env.render instead of bluebird.promisify
|
|
46
|
-
// vm-nunjucks uses vm2 which disallows access to object.prototype from
|
|
47
|
-
// code outside the vm this causes bluebird.promisify to throw a proxy error
|
|
48
|
-
function getNunjucksRender(env) {
|
|
49
|
-
return async (template, context) => new Promise((resolve, reject) => {
|
|
50
|
-
env.render(template, context, (error, str) => {
|
|
51
|
-
if (error) {
|
|
52
|
-
reject(error);
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
resolve(str ?? '');
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
export default function nunjucksFactory(fileLoader, config, stats) {
|
|
60
|
-
const loader = (filePath) => fileLoader(path.join(config.templatePath, filePath));
|
|
61
|
-
const templateLoader = new SyncLoader(loader);
|
|
62
|
-
const env = new nunjucks.Environment(templateLoader);
|
|
63
|
-
env.addFilter('date', createDateFilter(config));
|
|
64
|
-
env.addFilter('route', createRouteFilter(config, stats));
|
|
65
|
-
env.addFilter('md', markdownFilter);
|
|
66
|
-
const numberFilter = createNumberFilter(config);
|
|
67
|
-
env.addFilter('numberFormat', numberFilter);
|
|
68
|
-
env.addFilter('number', numberFilter);
|
|
69
|
-
env.addFilter('code', codeFilter);
|
|
70
|
-
env.addFilter('image', createImageFilter(config.imageBaseUrl, config.imageDefaults));
|
|
71
|
-
env.addFilter('asset', createAssetFilter(config.assetBaseUrl));
|
|
72
|
-
env.addFilter('intersection', intersectionFilter);
|
|
73
|
-
env.addFilter('union', unionFilter);
|
|
74
|
-
env.addFilter('pluralize', pluralizeFilter);
|
|
75
|
-
const render = getNunjucksRender(env);
|
|
76
|
-
return async (path, template, context) => {
|
|
77
|
-
try {
|
|
78
|
-
stats.pagesGenerated++;
|
|
79
|
-
return {
|
|
80
|
-
path: permalink(path),
|
|
81
|
-
contents: await render(template, deepClone(context))
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
catch (e) {
|
|
85
|
-
throw e instanceof TemplateRenderError ? e : formatError(e);
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
}
|
package/dist/paths.d.ts
DELETED
package/dist/paths.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
export function permalink(pathStr) {
|
|
3
|
-
if (pathStr.includes('.')) {
|
|
4
|
-
return pathStr;
|
|
5
|
-
}
|
|
6
|
-
return path.join(pathStr, 'index.html');
|
|
7
|
-
}
|
|
8
|
-
export function joinPath(...args) {
|
|
9
|
-
return path.join('/', ...args.filter(Boolean));
|
|
10
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { type ContextConfig, type FileLoader, type GraphQLQueryConfig, type RouteConfig } from './config.ts';
|
|
2
|
-
import type { Data, GraphQLConnector, Json, Stats } from './types.ts';
|
|
3
|
-
export type ResolvedQueryConfig = {
|
|
4
|
-
source: string;
|
|
5
|
-
} & GraphQLQueryConfig;
|
|
6
|
-
export type ResolveContextParams = {
|
|
7
|
-
contextConfig: ContextConfig;
|
|
8
|
-
srcPath: string;
|
|
9
|
-
fileLoader: FileLoader;
|
|
10
|
-
connector: GraphQLConnector;
|
|
11
|
-
route?: RouteConfig;
|
|
12
|
-
stats?: Stats;
|
|
13
|
-
};
|
|
14
|
-
type HandlerParams = {
|
|
15
|
-
srcPath: string;
|
|
16
|
-
filePath: string;
|
|
17
|
-
variables?: Record<string, Json>;
|
|
18
|
-
fileLoader: FileLoader;
|
|
19
|
-
connector: GraphQLConnector;
|
|
20
|
-
route?: RouteConfig;
|
|
21
|
-
stats?: Stats;
|
|
22
|
-
};
|
|
23
|
-
export declare function getType(path: string): string | null;
|
|
24
|
-
export type FileHandlerParams = Pick<HandlerParams, 'srcPath' | 'filePath' | 'fileLoader'>;
|
|
25
|
-
export declare function handleJSON({ srcPath, filePath, fileLoader }: FileHandlerParams): Promise<Data>;
|
|
26
|
-
export declare function handleYaml({ srcPath, filePath, fileLoader }: FileHandlerParams): Promise<Data>;
|
|
27
|
-
export declare function handleGraphQL({ srcPath, filePath, variables, connector, fileLoader, route, stats }: HandlerParams): Promise<Data>;
|
|
28
|
-
export declare function handleContextString(params: ResolveContextParams): Promise<Data | string>;
|
|
29
|
-
export declare function resolveGraphQLConfig(config: ContextConfig, { fileLoader, srcPath }: {
|
|
30
|
-
fileLoader: FileLoader;
|
|
31
|
-
srcPath: string;
|
|
32
|
-
}): Promise<ResolvedQueryConfig>;
|
|
33
|
-
export default function resolveContext(contextParams: ResolveContextParams): Promise<Data>;
|
|
34
|
-
export {};
|
package/dist/resolve-context.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import BbPromise from 'bluebird';
|
|
3
|
-
import yaml from 'js-yaml';
|
|
4
|
-
import forEach from 'lodash/forEach.js';
|
|
5
|
-
import { isGraphQLQueryConfig } from "./config.js";
|
|
6
|
-
import GraphQLError from "./errors/graphql-error.js";
|
|
7
|
-
import { recordContentUsage } from "./stats.js";
|
|
8
|
-
export function getType(path) {
|
|
9
|
-
const matches = /\.(graphql|yml|yaml|json)$/.exec(path);
|
|
10
|
-
if (matches?.length) {
|
|
11
|
-
const type = matches[1];
|
|
12
|
-
return type === 'yml' ? 'yaml' : type;
|
|
13
|
-
}
|
|
14
|
-
return null;
|
|
15
|
-
}
|
|
16
|
-
function getPath(contextConfig) {
|
|
17
|
-
if (typeof contextConfig === 'string') {
|
|
18
|
-
return contextConfig;
|
|
19
|
-
}
|
|
20
|
-
if (isGraphQLQueryConfig(contextConfig)) {
|
|
21
|
-
return contextConfig.query;
|
|
22
|
-
}
|
|
23
|
-
throw new Error('Unable to find file path in context config');
|
|
24
|
-
}
|
|
25
|
-
export async function handleJSON({ srcPath, filePath, fileLoader }) {
|
|
26
|
-
return fileLoader(path.join(srcPath, filePath)).then(JSON.parse);
|
|
27
|
-
}
|
|
28
|
-
export async function handleYaml({ srcPath, filePath, fileLoader }) {
|
|
29
|
-
const file = await fileLoader(path.join(srcPath, filePath));
|
|
30
|
-
const loaded = yaml.safeLoad(file);
|
|
31
|
-
if (!loaded || typeof loaded === 'string') {
|
|
32
|
-
throw new Error('unable to load file');
|
|
33
|
-
}
|
|
34
|
-
return loaded;
|
|
35
|
-
}
|
|
36
|
-
export async function handleGraphQL({ srcPath, filePath, variables, connector, fileLoader, route, stats }) {
|
|
37
|
-
const queryPath = path.join(srcPath, filePath);
|
|
38
|
-
const query = await fileLoader(queryPath);
|
|
39
|
-
const res = await connector({ query, variables }, { stats, source: filePath });
|
|
40
|
-
if (res.errors) {
|
|
41
|
-
throw new GraphQLError(`An error occurred while resolving ${queryPath}`, res.errors);
|
|
42
|
-
}
|
|
43
|
-
const data = res.data;
|
|
44
|
-
if (route?.path && !route.paginate) {
|
|
45
|
-
recordContentUsage(stats, data, route.path);
|
|
46
|
-
}
|
|
47
|
-
return data;
|
|
48
|
-
}
|
|
49
|
-
export async function handleContextString(params) {
|
|
50
|
-
const { contextConfig, ...rest } = params;
|
|
51
|
-
const filePath = getPath(contextConfig);
|
|
52
|
-
const type = getType(filePath);
|
|
53
|
-
if (type === 'json') {
|
|
54
|
-
return handleJSON({ ...rest, filePath });
|
|
55
|
-
}
|
|
56
|
-
if (type === 'yaml') {
|
|
57
|
-
return handleYaml({ ...rest, filePath });
|
|
58
|
-
}
|
|
59
|
-
if (type === 'graphql') {
|
|
60
|
-
return handleGraphQL({ ...rest, filePath });
|
|
61
|
-
}
|
|
62
|
-
return filePath;
|
|
63
|
-
}
|
|
64
|
-
export async function resolveGraphQLConfig(config, { fileLoader, srcPath }) {
|
|
65
|
-
let source;
|
|
66
|
-
let variables;
|
|
67
|
-
if (typeof config === 'string') {
|
|
68
|
-
source = config;
|
|
69
|
-
}
|
|
70
|
-
else if (isGraphQLQueryConfig(config)) {
|
|
71
|
-
source = config.query;
|
|
72
|
-
variables = config.variables;
|
|
73
|
-
}
|
|
74
|
-
if (!source) {
|
|
75
|
-
throw new Error('Missing .graphql or .gql query file');
|
|
76
|
-
}
|
|
77
|
-
const filePath = path.join(srcPath, source);
|
|
78
|
-
const query = await fileLoader(filePath);
|
|
79
|
-
return {
|
|
80
|
-
source,
|
|
81
|
-
query,
|
|
82
|
-
variables
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
async function handleValue(contextParams) {
|
|
86
|
-
const { contextConfig, ...rest } = contextParams;
|
|
87
|
-
if (typeof contextConfig === 'string') {
|
|
88
|
-
return handleContextString(contextParams);
|
|
89
|
-
}
|
|
90
|
-
if (typeof contextConfig === 'object') {
|
|
91
|
-
if (isGraphQLQueryConfig(contextConfig)) {
|
|
92
|
-
return handleGraphQL({
|
|
93
|
-
...rest,
|
|
94
|
-
filePath: contextConfig.query,
|
|
95
|
-
variables: contextConfig.variables
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
const context = {};
|
|
99
|
-
forEach(contextConfig, (value, key) => {
|
|
100
|
-
context[key] = handleValue({
|
|
101
|
-
...rest,
|
|
102
|
-
contextConfig: value
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
return BbPromise.props(context);
|
|
106
|
-
}
|
|
107
|
-
return contextConfig;
|
|
108
|
-
}
|
|
109
|
-
function isObject(obj) {
|
|
110
|
-
return Boolean(obj) && typeof obj === 'object' && !Array.isArray(obj);
|
|
111
|
-
}
|
|
112
|
-
export default async function resolveContext(contextParams) {
|
|
113
|
-
const context = await handleValue(contextParams);
|
|
114
|
-
return isObject(context) ? context : {};
|
|
115
|
-
}
|
package/dist/stats.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { Data, Json, Stats } from './types.ts';
|
|
2
|
-
export declare function generateWarning(stats: Stats | undefined, source: string, message: string): void;
|
|
3
|
-
export declare function scrapeIds(obj: Json, depth?: number, result?: Array<[string, number]>): Array<[string, number]>;
|
|
4
|
-
export declare function recordContentUsage(stats: Stats | undefined, data: Data, path: string): void;
|
package/dist/stats.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
export function generateWarning(stats, source, message) {
|
|
2
|
-
if (stats) {
|
|
3
|
-
if (!Array.isArray(stats.warnings)) {
|
|
4
|
-
stats.warnings = [];
|
|
5
|
-
}
|
|
6
|
-
stats.warnings.push({ source, message });
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
export function scrapeIds(obj, depth = 1, result = []) {
|
|
10
|
-
if (Array.isArray(obj)) {
|
|
11
|
-
for (const item of obj) {
|
|
12
|
-
scrapeIds(item, depth, result);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
else if (obj && typeof obj === 'object') {
|
|
16
|
-
const newDepth = depth + 1;
|
|
17
|
-
for (const key of Object.keys(obj)) {
|
|
18
|
-
const value = obj[key];
|
|
19
|
-
if (key === '_id' && typeof value === 'string') {
|
|
20
|
-
result.push([value, depth]);
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
scrapeIds(value, newDepth, result);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return result;
|
|
28
|
-
}
|
|
29
|
-
export function recordContentUsage(stats, data, path) {
|
|
30
|
-
if (stats?.contentUsage) {
|
|
31
|
-
const { contentUsage } = stats;
|
|
32
|
-
const ids = scrapeIds(data);
|
|
33
|
-
for (const [id, depth] of ids) {
|
|
34
|
-
contentUsage[id] ||= {};
|
|
35
|
-
const prevDepth = contentUsage[id][path];
|
|
36
|
-
contentUsage[id][path] = prevDepth ? Math.min(depth, prevDepth) : depth;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
package/dist/types.d.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import type { GraphQLFormattedError } from 'graphql';
|
|
2
|
-
type ArrayType<T extends any[]> = T extends Array<infer U> ? U : never;
|
|
3
|
-
export type WritableProps<T> = {
|
|
4
|
-
-readonly [P in keyof T]: T[P] extends any[] ? Array<WritableProps<ArrayType<T[P]>>> : WritableProps<T[P]>;
|
|
5
|
-
};
|
|
6
|
-
export type TemplateLocation = {
|
|
7
|
-
file: string;
|
|
8
|
-
line: number;
|
|
9
|
-
column: number;
|
|
10
|
-
};
|
|
11
|
-
export type JsonPrimitive = string | boolean | number | null;
|
|
12
|
-
export interface JsonMap<A> extends Record<string, A> {
|
|
13
|
-
}
|
|
14
|
-
export type Json = JsonPrimitive | Json[] | JsonMap<Json>;
|
|
15
|
-
export type Data = Record<string, any>;
|
|
16
|
-
export type Warning = {
|
|
17
|
-
source: string;
|
|
18
|
-
message: string;
|
|
19
|
-
};
|
|
20
|
-
export type ContentUsage = Record<string, Record<string, number>>;
|
|
21
|
-
export type Stats = {
|
|
22
|
-
warnings: Warning[];
|
|
23
|
-
pagesGenerated: number;
|
|
24
|
-
contentUsage?: ContentUsage;
|
|
25
|
-
};
|
|
26
|
-
export type TemplateContext = Record<string, Json>;
|
|
27
|
-
export type RenderedPage = {
|
|
28
|
-
path: string;
|
|
29
|
-
contents: string | Buffer;
|
|
30
|
-
};
|
|
31
|
-
export type RenderedRoute = {
|
|
32
|
-
pages: RenderedPage[];
|
|
33
|
-
order: number;
|
|
34
|
-
};
|
|
35
|
-
export type RenderTemplate = (path: string, template: string, context: TemplateContext) => Promise<RenderedPage>;
|
|
36
|
-
export type RenderTemplateDecorator = (renderTemplate: RenderTemplate) => RenderTemplate;
|
|
37
|
-
export type GraphQLConnectorParams = {
|
|
38
|
-
query: string;
|
|
39
|
-
variables?: Data;
|
|
40
|
-
operationName?: string;
|
|
41
|
-
};
|
|
42
|
-
export type GraphQLConnectorContext = {
|
|
43
|
-
source?: string;
|
|
44
|
-
stats?: Stats;
|
|
45
|
-
applyMigrations?: boolean;
|
|
46
|
-
isPaginated?: boolean;
|
|
47
|
-
};
|
|
48
|
-
export type GraphQLResult = {
|
|
49
|
-
errors?: GraphQLFormattedError[];
|
|
50
|
-
data?: Record<string, any> | null;
|
|
51
|
-
};
|
|
52
|
-
export type GraphQLConnector = (params: GraphQLConnectorParams, context?: GraphQLConnectorContext) => Promise<GraphQLResult>;
|
|
53
|
-
export {};
|
package/dist/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/util.d.ts
DELETED
package/dist/util.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export function updateIn(obj, path, update) {
|
|
2
|
-
const lastIndex = path.length - 1;
|
|
3
|
-
let current = obj;
|
|
4
|
-
for (let i = 0; i < path.length; i++) {
|
|
5
|
-
const key = path[i];
|
|
6
|
-
if (current[key] === undefined) {
|
|
7
|
-
break;
|
|
8
|
-
}
|
|
9
|
-
if (i === lastIndex) {
|
|
10
|
-
current[key] = update(current[key]);
|
|
11
|
-
}
|
|
12
|
-
else {
|
|
13
|
-
current = current[key];
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
return obj;
|
|
17
|
-
}
|