Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 12 additions & 0 deletions .oxfmtrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"ignorePatterns": [
"**/node_modules/**",
"**/build/**",
"**/coverage/**",
"**/cjs/**",
"**/es/**",
"**/types/**",
"**/*.hbs",
"**/test/fixtures/**"
]
}
29 changes: 29 additions & 0 deletions .oxlintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"plugins": ["node", "typescript"],
"rules": {
"eqeqeq": ["error", "always"],
"no-plusplus": ["error", {"allowForLoopAfterthoughts": true}],
"no-eval": "error",
"no-useless-call": "error",
"no-console": "error",
"no-shadow": "error",
"array-callback-return": "error",
"no-constructor-return": "error",
"no-promise-executor-return": "error",
"dot-notation": "error",
"no-var": "warn",
"no-unused-vars": "off",
"no-unused-expressions": "off",
"no-unused-private-class-members": "off",
"unicorn/no-thenable": "off"
},
"ignorePatterns": [
"**/node_modules/**",
"**/build/**",
"**/coverage/**",
"**/cjs/**",
"**/es/**",
"**/types/**",
"**/test/fixtures/**"
]
}
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ To add a new package to the repo:

## Test

- `yarn lint` run just eslint
- `yarn test` run lint and tests
- `yarn lint` run `oxlint` plus custom Ghost-specific lint checks
- `yarn lint:oxlint` run `oxlint` only
- `yarn lint:custom` run custom Ghost-specific lint checks
- `yarn format` format `js/ts/json/md` files with `oxfmt`
- `yarn format:check` check `js/ts/json/md` formatting with `oxfmt`
- `yarn test` run tests (many packages still run lint in `posttest`)


## Publish
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,31 @@
"workspaces": [
"packages/*"
],
"eslintIgnore": [
"**/node_modules/**"
],
"scripts": {
"dev": "echo \"Implement me!\"",
"main": "git checkout main && git pull && yarn",
"presetup": "yarn",
"setup": "yarn",
"test": "nx run-many -t test --parallel=10 --outputStyle=dynamic-legacy",
"test:ci": "nx run-many -t test --outputStyle=dynamic",
"lint": "nx run-many -t lint --outputStyle=dynamic",
"lint": "yarn lint:oxlint && yarn lint:custom",
"preship": "git diff --quiet && git diff --cached --quiet || (echo 'Error: working tree must be clean before shipping' && exit 1) && yarn test",
"ship": "nx release version --git-push --git-remote ${GHOST_UPSTREAM:-origin}",
"ship:patch": "yarn ship patch",
"ship:minor": "yarn ship minor",
"ship:major": "yarn ship major",
"ship:first-release": "yarn ship patch --first-release"
"ship:first-release": "yarn ship patch --first-release",
"lint:oxlint": "nx run-many -t lint --outputStyle=dynamic",
"format": "oxfmt -c .oxfmtrc.json \"packages/**/*.{js,ts,json,md}\"",
"format:check": "oxfmt -c .oxfmtrc.json --check \"packages/**/*.{js,ts,json,md}\"",
"lint:custom": "node scripts/lint-custom-rules.js"
},
"devDependencies": {
"@nx/js": "22.6.1",
"@vitest/coverage-v8": "3.2.4",
"eslint": "8.57.1",
"eslint-plugin-ghost": "3.5.0",
"nx": "22.6.1",
"oxfmt": "0.41.0",
"oxlint": "1.56.0",
"sinon": "21.0.3",
"ts-node": "10.9.2",
"vitest": "3.2.4"
Expand Down
7 changes: 0 additions & 7 deletions packages/.eslintrc.js

This file was deleted.

6 changes: 1 addition & 5 deletions packages/api-framework/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Each request goes through the following stages:

The framework we are building pipes a request through these stages in respect of the API controller configuration.


### Frame

Is a class, which holds all the information for request processing. We pass this instance by reference.
Expand Down Expand Up @@ -78,7 +77,6 @@ edit: {

#### Examples


```
edit: {
headers: {
Expand Down Expand Up @@ -142,13 +140,11 @@ edit: {
This is a monorepo package.

Follow the instructions for the top-level repo.

1. `git clone` this repo & `cd` into it as usual
2. Run `yarn` to install top-level dependencies.



## Test

- `yarn lint` run just eslint
- `yarn test` run lint and tests

2 changes: 1 addition & 1 deletion packages/api-framework/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
* @typedef {Record<string, ControllerMethod | string> & Record<'docName', string>} Controller
*/

module.exports = require('./lib/api-framework');
module.exports = require("./lib/api-framework");
28 changes: 14 additions & 14 deletions packages/api-framework/lib/Frame.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const debug = require('@tryghost/debug')('frame');
const _ = require('lodash');
const debug = require("@tryghost/debug")("frame");
const _ = require("lodash");

/**
* @description The "frame" holds all information of a request.
Expand Down Expand Up @@ -45,22 +45,22 @@ class Frame {
* Based on the API ctrl implemented, this fn will pick allowed properties to either options or data.
*/
configure(apiConfig) {
debug('configure');
debug("configure");

if (apiConfig.options) {
if (typeof apiConfig.options === 'function') {
if (typeof apiConfig.options === "function") {
apiConfig.options = apiConfig.options(this);
}

if (Object.prototype.hasOwnProperty.call(this.original, 'query')) {
if (Object.prototype.hasOwnProperty.call(this.original, "query")) {
Object.assign(this.options, _.pick(this.original.query, apiConfig.options));
}

if (Object.prototype.hasOwnProperty.call(this.original, 'params')) {
if (Object.prototype.hasOwnProperty.call(this.original, "params")) {
Object.assign(this.options, _.pick(this.original.params, apiConfig.options));
}

if (Object.prototype.hasOwnProperty.call(this.original, 'options')) {
if (Object.prototype.hasOwnProperty.call(this.original, "options")) {
Object.assign(this.options, _.pick(this.original.options, apiConfig.options));
}
}
Expand All @@ -71,19 +71,19 @@ class Frame {
this.data = _.cloneDeep(this.original.body);
} else {
if (apiConfig.data) {
if (typeof apiConfig.data === 'function') {
if (typeof apiConfig.data === "function") {
apiConfig.data = apiConfig.data(this);
}

if (Object.prototype.hasOwnProperty.call(this.original, 'query')) {
if (Object.prototype.hasOwnProperty.call(this.original, "query")) {
Object.assign(this.data, _.pick(this.original.query, apiConfig.data));
}

if (Object.prototype.hasOwnProperty.call(this.original, 'params')) {
if (Object.prototype.hasOwnProperty.call(this.original, "params")) {
Object.assign(this.data, _.pick(this.original.params, apiConfig.data));
}

if (Object.prototype.hasOwnProperty.call(this.original, 'options')) {
if (Object.prototype.hasOwnProperty.call(this.original, "options")) {
Object.assign(this.data, _.pick(this.original.options, apiConfig.data));
}
}
Expand All @@ -93,9 +93,9 @@ class Frame {
this.file = this.original.file;
this.files = this.original.files;

debug('original', this.original);
debug('options', this.options);
debug('data', this.data);
debug("original", this.original);
debug("options", this.options);
debug("data", this.data);
}

setHeader(header, value) {
Expand Down
16 changes: 8 additions & 8 deletions packages/api-framework/lib/api-framework.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
module.exports = {
get headers() {
return require('./headers');
return require("./headers");
},

get http() {
return require('./http');
return require("./http");
},

get Frame() {
return require('./Frame');
return require("./Frame");
},

get pipeline() {
return require('./pipeline');
return require("./pipeline");
},

get validators() {
return require('./validators');
return require("./validators");
},

get serializers() {
return require('./serializers');
return require("./serializers");
},

get utils() {
return require('./utils');
}
return require("./utils");
},
};
60 changes: 35 additions & 25 deletions packages/api-framework/lib/headers.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const url = require('url');
const debug = require('@tryghost/debug')('headers');
const INVALIDATE_ALL = '/*';
const url = require("url");
const debug = require("@tryghost/debug")("headers");
const INVALIDATE_ALL = "/*";

const cacheInvalidate = (result, options = {}) => {
let value = options.value;

return {
'X-Cache-Invalidate': value || INVALIDATE_ALL
"X-Cache-Invalidate": value || INVALIDATE_ALL,
};
};

Expand All @@ -21,13 +21,13 @@ const disposition = {
csv(result, options = {}) {
let value = options.value;

if (typeof options.value === 'function') {
if (typeof options.value === "function") {
value = options.value();
}

return {
'Content-Disposition': `Attachment; filename="${value}"`,
'Content-Type': 'text/csv'
"Content-Disposition": `Attachment; filename="${value}"`,
"Content-Type": "text/csv",
};
},

Expand All @@ -40,9 +40,9 @@ const disposition = {
*/
json(result, options = {}) {
return {
'Content-Disposition': `Attachment; filename="${options.value}"`,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(JSON.stringify(result))
"Content-Disposition": `Attachment; filename="${options.value}"`,
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(JSON.stringify(result)),
};
},

Expand All @@ -55,9 +55,9 @@ const disposition = {
*/
yaml(result, options = {}) {
return {
'Content-Disposition': `Attachment; filename="${options.value}"`,
'Content-Type': 'application/yaml',
'Content-Length': Buffer.byteLength(JSON.stringify(result))
"Content-Disposition": `Attachment; filename="${options.value}"`,
"Content-Type": "application/yaml",
"Content-Length": Buffer.byteLength(JSON.stringify(result)),
};
},

Expand All @@ -79,18 +79,18 @@ const disposition = {
.then(() => {
let value = options.value;

if (typeof options.value === 'function') {
if (typeof options.value === "function") {
value = options.value();
}

return value;
})
.then((filename) => {
return {
'Content-Disposition': `Attachment; filename="${filename}"`
"Content-Disposition": `Attachment; filename="${filename}"`,
};
});
}
},
};

module.exports = {
Expand All @@ -106,15 +106,21 @@ module.exports = {
let headers = {};

if (apiConfigHeaders.disposition) {
const dispositionHeader = await disposition[apiConfigHeaders.disposition.type](result, apiConfigHeaders.disposition);
const dispositionHeader = await disposition[apiConfigHeaders.disposition.type](
result,
apiConfigHeaders.disposition,
);

if (dispositionHeader) {
Object.assign(headers, dispositionHeader);
}
}

if (apiConfigHeaders.cacheInvalidate) {
const cacheInvalidationHeader = cacheInvalidate(result, apiConfigHeaders.cacheInvalidate);
const cacheInvalidationHeader = cacheInvalidate(
result,
apiConfigHeaders.cacheInvalidate,
);

if (cacheInvalidationHeader) {
Object.assign(headers, cacheInvalidationHeader);
Expand All @@ -123,15 +129,19 @@ module.exports = {

const locationHeaderDisabled = apiConfigHeaders?.location === false;
const hasLocationResolver = apiConfigHeaders?.location?.resolve;
const hasFrameData = (frame?.method === 'add' || hasLocationResolver) && result[frame.docName]?.[0]?.id;
const hasFrameData =
(frame?.method === "add" || hasLocationResolver) && result[frame.docName]?.[0]?.id;

if (!locationHeaderDisabled && hasFrameData) {
const protocol = (frame.original.url.secure === false) ? 'http://' : 'https://';
const protocol = frame.original.url.secure === false ? "http://" : "https://";
const resourceId = result[frame.docName][0].id;

let locationURL = url.resolve(`${protocol}${frame.original.url.host}`,frame.original.url.pathname);
if (!locationURL.endsWith('/')) {
locationURL += '/';
let locationURL = url.resolve(
`${protocol}${frame.original.url.host}`,
frame.original.url.pathname,
);
if (!locationURL.endsWith("/")) {
locationURL += "/";
}

locationURL += `${resourceId}/`;
Expand All @@ -141,7 +151,7 @@ module.exports = {
}

const locationHeader = {
Location: locationURL
Location: locationURL,
};

Object.assign(headers, locationHeader);
Expand All @@ -153,5 +163,5 @@ module.exports = {

debug(headers);
return headers;
}
},
};
Loading
Loading