Skip to content

Commit

Permalink
Support commonjs
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed Jan 1, 2019
1 parent 653f72f commit d09cc7d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
15 changes: 11 additions & 4 deletions src/adapter/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export interface HttpError extends Error {
response: { body?: string }
}

interface Endpoint {
uri: string
}

const createError = ({ statusCode, message }: HttpError, uri: string) => {
switch (statusCode) {
case 404: return { status: 'notfound', error: `Could not find the url ${uri}` }
Expand All @@ -19,18 +23,21 @@ const generateHeaders = (method: string) => (method === 'POST')
? { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }
: {}

const isEndpointWithUri = (endpoint?: Endpoint | null): endpoint is Endpoint =>
!!(endpoint && endpoint.uri)

const send = (got: GotFn) => async (request: Request): Promise<Response> => {
const { endpoint, data } = request
if (!endpoint || !endpoint.uri) {

if (!isEndpointWithUri(endpoint)) {
return { status: 'error', error: 'Request has no endpoint or uri' }
}

const method = resolveMethod(data)
const headers = generateHeaders(method)
const { uri } = endpoint

try {
const response = await got(uri, {
const response = await got(endpoint.uri, {
method,
body: data as string,
headers
Expand All @@ -41,7 +48,7 @@ const send = (got: GotFn) => async (request: Request): Promise<Response> => {
data: response.body
}
} catch (error) {
return { ...createError(error, uri), data: error.response && error.response.body }
return { ...createError(error, endpoint.uri), data: error.response && error.response.body }
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import adapter from './adapter'
import form from './adapter'

export default {
adapter
const adapters = {
form
}

export = {
adapters,
default: {
adapters
}
}
9 changes: 9 additions & 0 deletions src/tests/commonjs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import test from 'ava'

import resources = require('..')

test('should have resources', (t) => {
t.truthy(resources)
t.truthy(resources.adapters)
t.truthy(resources.adapters.form)
})
3 changes: 2 additions & 1 deletion src/tests/get.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import test from 'ava'
import nock = require('nock')

import adapter from '../adapter'
import resources from '..'
const { form: adapter } = resources.adapters

// Helpers

Expand Down
3 changes: 2 additions & 1 deletion src/tests/set.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import test from 'ava'
import nock = require('nock')

import adapter from '../adapter'
import resources from '..'
const { form: adapter } = resources.adapters

// Helpers

Expand Down

0 comments on commit d09cc7d

Please sign in to comment.