Skip to content

Commit

Permalink
Merge pull request #296 from AmazeeLabs/SLB-445-waku-decap
Browse files Browse the repository at this point in the history
SLB-445: Integrate Waku with Decap
  • Loading branch information
pmelab authored Sep 3, 2024
2 parents faa5343 + 2687ea5 commit 5de3d47
Show file tree
Hide file tree
Showing 42 changed files with 2,677 additions and 1,531 deletions.
3 changes: 3 additions & 0 deletions apps/cms/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ autoload.json

# A workaround to avoid turbo caching locally.
turbo-seed.txt

# Executor typescript build
dist
13 changes: 10 additions & 3 deletions apps/cms/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
{
"$schema": "https://json.schemastore.org/package.json",
"name": "@custom/cms",
"version": "1.0.0",
"main": "index.js",
"main": "./dist/index.js",
"license": "MIT",
"private": true,
"types": "./dist/index.d.ts",
"type": "module",
"description": "Drupal based content management system.",
"sideEffects": false,
"scripts": {
"prep:composer": "if command -v composer; then COMPOSER_ALLOW_SUPERUSER=1 composer install; else echo 'Skipping composer install.'; fi",
"prep:database": "./prep-database.sh",
"prep:schema": "pnpm drush silverback-gatsby:schema-export ../../../tests/schema || true",
"prep:scripts": "tsc",
"fix-premissions": "chmod +w web/sites/default/files/.htaccess && chmod +w web/sites/default/files/private/.htaccess",
"drush": "SB_ENVIRONMENT=1 SIMPLETEST_DB=sqlite://localhost/sites/default/files/.sqlite DRUSH_OPTIONS_URI=http://127.0.0.1:8888 vendor/bin/drush",
"silverback": "SB_ENVIRONMENT=1 SIMPLETEST_DB=sqlite://localhost/sites/default/files/.sqlite SB_ADMIN_USER=admin SB_ADMIN_PASS=admin vendor/bin/silverback",
Expand All @@ -34,7 +38,10 @@
"@custom/gutenberg_blocks": "workspace:*",
"@custom/schema": "workspace:*",
"@custom/test_content": "workspace:*",
"@custom/ui": "workspace:*",
"@custom/preview": "workspace:*"
"@custom/ui": "workspace:*"
},
"devDependencies": {
"@types/node": "^22.0.0",
"@types/react": "^18.3.3"
}
}
62 changes: 62 additions & 0 deletions apps/cms/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { AnyOperationId, OperationVariables } from '@custom/schema';

export function createDrupalExecutor(host: string) {
return async function <OperationId extends AnyOperationId>(
id: OperationId,
variables?: OperationVariables<OperationId>,
) {
const url = new URL(`${host}/graphql`);
const isMutation = id.includes('Mutation:');
const publicUrl =
typeof window !== 'undefined'
? new URL(window.location.href)
: new URL(process.env.WAKU_PUBLIC_URL || 'http://127.0.0.1:8000');
const headers = {
'SLB-Forwarded-Proto': publicUrl.protocol.slice(0, -1),
'SLB-Forwarded-Host': publicUrl.hostname,
'SLB-Forwarded-Port': publicUrl.port,
'X-Forwarded-Proto': publicUrl.protocol.slice(0, -1),
'X-Forwarded-Host': publicUrl.hostname,
'X-Forwarded-Port': publicUrl.port,
};

const requestInit = (
isMutation
? {
method: 'POST',
credentials: 'include',
body: JSON.stringify({
queryId: id,
variables: variables || {},
}),
headers: {
...headers,
'Content-Type': 'application/json',
},
}
: {
credentials: 'include',
headers,
}
) satisfies RequestInit;

if (!isMutation) {
url.searchParams.set('queryId', id);
url.searchParams.set('variables', JSON.stringify(variables || {}));
}

try {
const { data, errors } = await (await fetch(url, requestInit)).json();
if (errors) {
console.error('GraphQL error:', errors);
if (!data) {
throw new Error('GraphQL error: ' + JSON.stringify(errors));
}
}
return data;
} catch (error) {
console.error('Fetch error:', error);
throw new Error(`Fetch error: ${error}`);
}
};
}
23 changes: 23 additions & 0 deletions apps/cms/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "NodeNext",
"declaration": true,
"moduleResolution": "NodeNext",
"resolveJsonModule": true,
"isolatedModules": true,
"checkJs": true,
"outDir": "dist",
"jsx": "react-jsx"
},
"include": ["src/**/*"]
}
8 changes: 7 additions & 1 deletion apps/cms/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"extends": ["//"],
"tasks": {
"prep": {
"dependsOn": ["prep:schema"]
"dependsOn": ["prep:scripts", "prep:schema"]
},
"prep:scripts": {
"dependsOn": ["^prep"],
"inputs": ["src/**"],
"outputs": ["dist/**"]
},
"prep:schema": {
"dependsOn": ["prep:database"]
Expand Down Expand Up @@ -37,6 +42,7 @@
"test:integration": {
"dependsOn": [
"prep",
"@custom/preview#prep",
"@custom-tests/e2e#test:integration:drupal",
"@custom-tests/schema#test:integration"
]
Expand Down
13 changes: 12 additions & 1 deletion apps/decap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"private": true,
"version": "0.0.0",
"type": "module",
"main": "build/index.js",
"types": "build/index.d.ts",
"scripts": {
"dev": "vite --host",
"prep:vite": "vite build",
Expand All @@ -17,13 +19,18 @@
"dependencies": {
"@amazeelabs/cloudinary-responsive-image": "^1.6.15",
"@amazeelabs/gatsby-plugin-static-dirs": "^1.0.1",
"@amazeelabs/graphql-directives": "^1.3.2",
"@amazeelabs/graphql-directives": "^1.3.7",
"@custom/schema": "workspace:*",
"@custom/ui": "workspace:*",
"decap-cms-app": "^3.0.12",
"decap-cms-core": "^3.2.8",
"deepmerge": "^4.3.1",
"graphql": "16.8.1",
"image-size": "^1.1.1",
"lodash": "^4.17.21",
"mime-types": "^2.1.35",
"nanoid": "^5.0.4",
"object-deep-merge": "^1.0.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rehype-sanitize": "^5.0.1",
Expand All @@ -36,12 +43,16 @@
},
"devDependencies": {
"@amazeelabs/decap-cms-backend-token-auth": "^1.1.7",
"@types/deepmerge": "^2.2.0",
"@types/lodash": "^4.17.6",
"@types/mime-types": "^2.1.4",
"@types/node": "^18",
"@types/react": "^18.2.46",
"@types/react-dom": "^18.2.18",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"@vitejs/plugin-react-swc": "^3.5.0",
"esbuild-plugin-raw": "^0.1.8",
"netlify-cms-proxy-server": "^1.3.24",
"tsup": "^8.0.1",
"typescript": "^5.3.3",
Expand Down
60 changes: 50 additions & 10 deletions apps/decap/src/collections/page.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,54 @@
import { expect, test, vi } from 'vitest';
import { ListPagesQuery, ViewPageQuery } from '@custom/schema';
import { expect, test } from 'vitest';
import { z } from 'zod';

import { getPages } from '..';
import { createExecutor } from '../graphql';
import { pageResolvers } from './page';

vi.mock('../helpers/path', () => ({
path: `${new URL(import.meta.url).pathname
.split('/')
.slice(0, -1)
.join('/')}/../..`,
}));
const exec = createExecutor([pageResolvers('./')]);

test('getPages', () => {
expect(() => getPages()).not.toThrow();
const listPagesSchema = z.object({
ssgPages: z.object({
rows: z.array(
z.object({ translations: z.array(z.object({ path: z.string() })) }),
),
total: z.number(),
}),
});

test('retrieve all pages', async () => {
const result = await exec(ListPagesQuery, { args: '' });
const parsed = listPagesSchema.safeParse(result);

expect(parsed.success).toBe(true);
});

test('load a page by path', async () => {
const list = listPagesSchema.parse(await exec(ListPagesQuery, { args: '' }));

const path = list.ssgPages.rows[0].translations[0].path;

const result = await exec(ViewPageQuery, { pathname: path });

const parsed = z
.object({
page: z.object({
path: z.string(),
title: z.string(),
locale: z.string(),
translations: z.array(
z.object({
locale: z.string(),
path: z.string(),
}),
),
}),
})
.safeParse(result);

if (!parsed.success) {
console.error(parsed.error);
}
expect(parsed.success).toBe(true);
expect(parsed.data?.page.path).toEqual(path);
});
Loading

0 comments on commit 5de3d47

Please sign in to comment.