Skip to content

Commit

Permalink
feat(Galleries): React or API access galleries the Next way
Browse files Browse the repository at this point in the history
  • Loading branch information
danactive committed Jun 6, 2021
1 parent 78d0990 commit 6bd2df1
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion api/server/plugins/walk/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const routes = require('../../../lib/routes');
const files = require('../../../../../app/src/lib/filesystem');
const files = require('../../../../../app/src/lib/filesystems');
const validation = require('../../../lib/validation');

const routeWalkPath = {
Expand Down
1 change: 1 addition & 0 deletions app/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
semi: [2, 'never'], // remove semicolons
'react/prop-types': 'off',
'react/react-in-jsx-scope': 'off', // Next.js magically includes
'react/jsx-one-expression-per-line': 'off', // too vertical
},
env: {
jest: true,
Expand Down
1 change: 1 addition & 0 deletions app/__tests__/__snapshots__/snapshot.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exports[`renders homepage unchanged 1`] = `
className="container"
>
<main>
<h1 />
<h1
className="title"
>
Expand Down
1 change: 1 addition & 0 deletions app/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = {
'!**/node_modules/**',
],
setupFilesAfterEnv: ['<rootDir>/setupTests.js'],
testEnvironment: 'jsdom',
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '!**/__fixtures__/**'],
testPathIgnorePatterns: ['/node_modules/', '/.next/'],
transform: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import local from '../../../../src/lib/filesystem'
import local from '../../../../src/lib/filesystems'

const errorSchema = (message) => ({ files: [], error: { message } })

Expand Down
1 change: 1 addition & 0 deletions app/pages/api/galleries/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('Galleries API', () => {
const result = await response.json()

expect(response.status).toBe(405)
expect(result.error.message.toLowerCase()).toContain('not allowed')

expect(result.galleries.length).toBe(0)
expect(result.galleries.includes('demo')).toBeFalsy()
Expand Down
6 changes: 2 additions & 4 deletions app/pages/api/galleries/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import local from '../../../src/lib/galleries'

const errorSchema = (message) => ({ galleries: [], error: { message } })

export default async function handler({ method }, res) {
switch (method) {
case 'GET': {
const out = await local.get(errorSchema)
const out = await local.get(true)
return res.status(out.status).json(out.body)
}
default:
return res.status(405).json(errorSchema(`Method ${method} Not Allowed`))
return res.status(405).json(local.errorSchema(`Method ${method} Not Allowed`))
}
}
11 changes: 10 additions & 1 deletion app/pages/index.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import Head from 'next/head'

import styles from './index.module.css'
import { get as getGalleries } from '../src/lib/galleries'

export async function getStaticProps() {
return {
props: await getGalleries(),
}
}

const Home = () => (
const Home = ({ galleries }) => (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
</Head>

<main>
<h1>Galleries {JSON.stringify(galleries)}</h1>
<h1 className={styles.title}>
Welcome to
{' '}
Expand Down
File renamed without changes.
27 changes: 24 additions & 3 deletions app/src/lib/galleries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,40 @@ const fsCallback = require('fs')

const fs = fsCallback.promises

async function get(errorSchema = (msg) => msg) {
const errorSchema = (message) => {
const out = { galleries: [] }
if (!message) return out
return { ...out, error: { message } }
}

/**
* Get Galleries from local filesystem
* @param {boolean} returnEnvelope will enable a return value with HTTP status code and body
* @returns {Promise} galleries
*/
async function get(returnEnvelope = false) {
try {
const hasPrefix = (content) => content.isDirectory()
const namesOnly = (content) => content.name

const contents = await fs.readdir('../public/galleries', { withFileTypes: true })
const galleries = contents.filter(hasPrefix).map(namesOnly)

return { body: { galleries: contents.filter(hasPrefix).map(namesOnly) }, status: 200 }
if (returnEnvelope) {
return { body: { galleries }, status: 200 }
}

return { galleries }
} catch (e) {
return { body: errorSchema('No galleries are found'), status: 404 }
if (returnEnvelope) {
return { body: errorSchema('No galleries are found'), status: 404 }
}

return errorSchema()
}
}

module.exports = {
get,
errorSchema,
}

0 comments on commit 6bd2df1

Please sign in to comment.