Skip to content

Instantly share code, notes, and snippets.

@inodaf
Last active March 20, 2018 13:28
Show Gist options
  • Save inodaf/f6a7f42a488ce09773b931ad33262b80 to your computer and use it in GitHub Desktop.
Save inodaf/f6a7f42a488ce09773b931ad33262b80 to your computer and use it in GitHub Desktop.
Testing Defy.js
defmodule('Contestation', ({ def, run, callbacks }) => {
const { Http } = Defy.Plugins
const storageURL = `${process.env.SANDFILE_URL}/document`
const graphQLURL = process.env.GRAPHQL_URL
def('post_document', doc => {
const requestPayload = {
query: mutationBuilder(doc, 'addDocument', 'errors { code path description } }')
}
Http.post(graphQLURL, requestPayload)
.then(rs => callback())
})
def('format_response', response => {
const [url, documentType] = response
return { url, documentType, contestationId }
})
def('link_documents', async (...queue) => {
const resolvedQueue = await Promise.all(queue)
resolvedQueue
.map(d => run('format_response', d))
.map(d => run('post_document', d))
})
def('send_document', ({ mpa, contestation, file, name }) => {
const requestHeaders = { headers: { mpa, contestation, name } }
const requestPayload = new FormData()
requestPayload.append('document', file)
return new Promise((resolve, reject) => {
Http.post(storageURL, requestPayload, requestHeaders)
.then(v => resolve([v.body.link, name]))
.catch(e => reject(e))
})
})
def('do_contest', documents => {
const promiseQueue = documents.map(d => run('send_document', d))
return run('link_documents', promiseQueue)
})
})
// Importing Plugins.
const Http = require('defy-http')
// Attaching plugins to Root.
Defy.use({ Http })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment