Skip to content

Commit

Permalink
feat(Exists): API and Next app work well together
Browse files Browse the repository at this point in the history
  • Loading branch information
danactive committed May 14, 2021
1 parent 0fb2613 commit 96bfb76
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 100 deletions.
3 changes: 0 additions & 3 deletions api/server/plugins/exists/test/.eslintrc.js

This file was deleted.

72 changes: 0 additions & 72 deletions api/server/plugins/exists/test/exists.spec.js

This file was deleted.

2 changes: 1 addition & 1 deletion api/server/plugins/generatePreview/lib/resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs');
const sharp = require('sharp');

const { previewFolderName } = require('../../../../../config.json');
const existsMod = require('../../exists/lib/exists');
const existsMod = require('../../../../../app/pages/api/src/exists');
const utils = require('../../utils');

async function transformImages(originalPath) {
Expand Down
2 changes: 1 addition & 1 deletion api/server/plugins/rename/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const existsChecker = require('../../exists/lib/exists');
const existsChecker = require('../../../../../app/pages/api/src/exists');
const filenamer = require('./filenames');
const renamer = require('./rename');
const routes = require('../../../lib/routes');
Expand Down
2 changes: 1 addition & 1 deletion api/server/plugins/rename/lib/rename.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const Boom = require('boom');
const fs = require('fs');
const path = require('path');

const exists = require('../../exists/lib/exists');
const exists = require('../../../../../app/pages/api/src/exists');
const utils = require('../../utils');

/*
Expand Down
2 changes: 1 addition & 1 deletion api/server/plugins/rename/test/rename.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ tape('Verify rename library', { skip: false }, (describe) => {
const appRoot = require('app-root-path');
const path = require('path');

const exist = require('../../exists/lib/exists');
const exist = require('../../../../../app/pages/api/src/exists');
const plugin = require('../lib/rename');
const utils = require('../../utils');

Expand Down
2 changes: 1 addition & 1 deletion api/server/plugins/resize/lib/resize.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const boom = require('boom');
const gm = require('gm');

const existsMod = require('../../exists/lib/exists');
const existsMod = require('../../../../../app/pages/api/src/exists');
const utils = require('../../utils');

/*
Expand Down
2 changes: 1 addition & 1 deletion api/server/plugins/resize/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const tape = require('tape-catch');

const existsChecker = require('../../exists/lib/exists');
const existsChecker = require('../../../../../app/pages/api/src/exists');

tape('Verify /resize route', { skip: false }, (describe) => {
const calipers = require('calipers')('jpeg');
Expand Down
2 changes: 1 addition & 1 deletion api/server/plugins/resize/test/resize.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const tape = require('tape-catch');
const existsChecker = require('../../exists/lib/exists');
const existsChecker = require('../../../../../app/pages/api/src/exists');

tape('Verify resize library', { skip: false }, (describe) => {
const calipers = require('calipers')('jpeg');
Expand Down
13 changes: 13 additions & 0 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"dependencies": {
"app-root-path": "^3.0.0",
"boom": "^7.0.0",
"glob": "^7.1.6",
"mime-types": "^2.1.28",
"next": "^10.0.0",
Expand Down
File renamed without changes.
73 changes: 73 additions & 0 deletions app/pages/api/src/__tests__/exists.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import path from 'path'

import handler from '../exists'

describe('Exists library', () => {
const errorSchema = (message) => message

describe('Expect result', () => {
const successTest = async (expect, testPath) => {
expect.hasAssertions()
try {
const verifiedPath = await handler.pathExists(testPath, errorSchema)

const normalTestPath = path.normalize(testPath)
expect(verifiedPath.endsWith(normalTestPath)).toBeTruthy()
} catch (error) {
expect(error).toBeUndefined()
}
}

test('* Real relative file exists', async () => {
const testPath = 'test/fixtures/exists.txt'
await successTest(expect, testPath)
})

test('* Real relative folder exists', async () => {
const testPath = 'test/fixtures'
await successTest(expect, testPath)
})

test('* Real absolute file exists', async () => {
const testPath = '/test/fixtures/exists.txt'
await successTest(expect, testPath)
})

test('* Real absolute folder exists', async () => {
const testPath = '/test/fixtures'
await successTest(expect, testPath)
})

test('* Real root absolute file exists', async () => {
const testPath = path.join(__dirname, '../../../../../public/test/fixtures/exists.txt')
await successTest(expect, testPath)
})

test('* Real root absolute folder exists', async () => {
const testPath = path.join(__dirname, '../../../../../public/test/fixtures')
await successTest(expect, testPath)
})
})

describe('Expect result', () => {
const failureTest = async (expect, testPath) => {
expect.hasAssertions()
try {
const verifiedPath = await handler.pathExists(testPath)
expect(verifiedPath).toBeUndefined()
} catch (error) {
expect(error.isBoom).toBeTruthy()
}
}

test('* Fake absolute path does not exists', async () => {
const testPath = '/test/fixtures/fakeFolder'
await failureTest(expect, testPath)
})

test('* Safe path throws error', async () => {
const testPath = true
await failureTest(expect, testPath)
})
})
})
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const boom = require('boom');
const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const boom = require('boom')
const fs = require('fs')
const path = require('path')
const { promisify } = require('util')

const getStat = promisify(fs.stat);
const utils = require('../../utils');
const getStat = promisify(fs.stat)
const utilsFactory = require('../admin/filesystem/utils')

const MODULE_NAME = 'pathExists';
const MODULE_NAME = 'pathExists'

/*
Verify if a path exists on the file system
Expand All @@ -15,29 +15,31 @@ Verify if a path exists on the file system
@param {string} verifyPath relative/absolute path (file or folder) on the file system
@returns {Promise} root absolute path
*/
const pathExists = async (verifyPath) => {
const pathExists = async (verifyPath, errorSchema) => {
if (verifyPath === undefined || verifyPath === null) {
throw boom.notFound(`${MODULE_NAME}: File system path is missing (${verifyPath})`);
throw boom.notFound(`${MODULE_NAME}: File system path is missing (${verifyPath})`)
}

const utils = utilsFactory(errorSchema)

try {
const verifiedPath = utils.file.safePublicPath(verifyPath);
const stats = await getStat(verifiedPath);
const verifiedPath = utils.safePublicPath(verifyPath)
const stats = await getStat(verifiedPath)

if (stats.isFile() || stats.isDirectory()) {
return verifiedPath;
return verifiedPath
}

throw boom.notFound('File failed');
throw boom.notFound('File failed')
} catch (error) {
if (typeof verifyPath === 'string' || verifyPath instanceof String) {
const pathType = path.isAbsolute(verifyPath) ? 'absolute' : 'relative';
const pathType = path.isAbsolute(verifyPath) ? 'absolute' : 'relative'

throw boom.notFound(`${MODULE_NAME}: File system path is ${pathType} and not found due to error (${error})`);
throw boom.notFound(`${MODULE_NAME}: File system path is ${pathType} and not found due to error (${error})`)
} else {
throw boom.notFound(`${MODULE_NAME}: File system path is not found due to error (${error})`);
throw boom.notFound(`${MODULE_NAME}: File system path is not found due to error (${error})`)
}
}
};
}

module.exports = { pathExists };
module.exports = { pathExists }

0 comments on commit 96bfb76

Please sign in to comment.