Skip to content

Commit

Permalink
Declare schema and use symbol for mapping members
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBuchholz committed Feb 18, 2020
1 parent 5e1d195 commit 77b913f
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const util = require('util')

const readApis = 'readdir,readFile,stat'.split(',')
const writeApis = 'mkdir,rmdir,unlink,writeFile'.split(',')
const $apis = Symbol('REserve/fs@apis')

const fsAsync = {}
readApis.concat(writeApis).forEach(api => {
Expand Down Expand Up @@ -76,10 +77,10 @@ function wrap (api, result) {
}

const handlers = {}
handlers.GET = async ({ allApis, mapping, request, response }) => {
handlers.GET = async ({ mapping, request, response }) => {
send(response, clientTemplate
.replace('<APIS>', allApis.join(','))
.replace('<NAME>', mapping['client-name'] || 'fs')
.replace('<APIS>', mapping[$apis].join(','))
.replace('<NAME>', mapping['client-name'])
.replace('<URL>', request.url)
)
}
Expand All @@ -98,9 +99,9 @@ function forwardToFs (call, response) {
.then(answer => send(response, answer))
}

handlers.POST = async ({ allApis, mapping, match, redirect, request, response }) => {
handlers.POST = async ({ mapping, match, redirect, request, response }) => {
const call = JSON.parse(await readBody(request))
if (!allApis.includes(call.api)) {
if (!mapping[$apis].includes(call.api)) {
return 404
}
call.args[0] = path.join(redirect, call.args[0])
Expand All @@ -111,17 +112,30 @@ handlers.POST = async ({ allApis, mapping, match, redirect, request, response })
}

module.exports = {
async redirect ({ mapping, match, redirect, request, response }) {
let allApis = readApis
schema: {
'client-name': {
type: 'string',
defaultValue: 'fs'
},
'read-only': {
type: 'boolean',
defaultValue: false
}
},

async validate (mapping) {
if (!mapping['read-only']) {
allApis = allApis.concat(writeApis)
mapping[$apis] = readApis.concat(writeApis)
} else {
mapping[$apis] = readApis
}
},

async redirect ({ mapping, match, redirect, request, response }) {
const handler = handlers[request.method]
if (handler) {
return handler({ allApis, mapping, match, redirect, request, response })
return handler({ mapping, match, redirect, request, response })
}

return 500
}
}

0 comments on commit 77b913f

Please sign in to comment.