Skip to content

Commit b2e743e

Browse files
committed
Use swagger client instead of isomorphic fetch
1 parent d6f1ac1 commit b2e743e

File tree

7 files changed

+122
-29
lines changed

7 files changed

+122
-29
lines changed

api/swagger.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ produces:
1313
paths:
1414
/users/{userId}:
1515
get:
16+
operationId: findUserById
1617
description: Returns user object
1718
parameters:
1819
- name: userId

lib/api/Client.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as Swagger from 'swagger-client'
2+
import * as fs from 'fs'
3+
import * as jsYaml from 'js-yaml'
4+
import * as nock from 'nock'
5+
6+
const API_HOST = process.env.API_HOST
7+
8+
function loadSpec (): any {
9+
const spec = fs.readFileSync(__dirname + '/../../api/swagger.yml', 'utf8')
10+
return { ...jsYaml.safeLoad(spec), host: API_HOST }
11+
}
12+
13+
export async function execute (operationId: String, parameters: any): Promise<any> {
14+
const spec = loadSpec()
15+
const { body } = await Swagger.execute({ spec, operationId, parameters })
16+
return body
17+
}
18+
19+
export function mockExecute (operationId: String, parameters: any): any {
20+
const spec = loadSpec()
21+
const { method, url } = Swagger.buildRequest({ spec, operationId, parameters })
22+
return nock(url).intercept('', method)
23+
}

lib/user/HTTPUserGateway.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as HTTPUserGateway from './HTTPUserGateway'
2-
import * as nock from 'nock'
2+
import { mockExecute } from '../api/Client'
33

44
const API_ORIGIN = process.env.API_ORIGIN
55

66
describe('HTTPUserGateway', () => {
77
describe('when finding by id', () => {
88
test('user has name', async () => {
9-
nock(API_ORIGIN).get('/api/users/guid').reply(200, { name: 'Jim', email: '[email protected]' })
9+
mockExecute('findUserById', { userId: 'guid' }).reply(200, { name: 'Jim', email: '[email protected]' })
1010
const user = await HTTPUserGateway.findById('guid')
1111
expect(user.name).toBe('Jim')
1212
expect(user.email).toBe('[email protected]')

lib/user/HTTPUserGateway.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import 'isomorphic-fetch'
1+
import { execute } from '../api/Client'
22
import User from './User'
33

44
const API_ORIGIN = process.env.API_ORIGIN
55

6-
export async function findById (id: string) : Promise<User> {
7-
const res = await fetch(`${API_ORIGIN}/api/users/${id}`)
8-
const { name, email } = await res.json()
6+
export async function findById (userId: string) : Promise<User> {
7+
const { name, email } = await execute('findUserById', { userId })
98
return new User(name, email)
109
}

package-lock.json

Lines changed: 84 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"scripts": {
33
"build:ts": "tsc -p tsconfig.production.json",
4-
"build:next": "API_ORIGIN=http://localhost:8080 next build",
4+
"build:next": "API_HOST=localhost:8080 next build",
55
"prepare": "npm run clean && npm run build:ts && npm run build:next",
6-
"start": "NODE_ENV=production API_ORIGIN=http://localhost:8080 republic lib/app.js",
7-
"dev": "tsc && NODE_ENV=development API_ORIGIN=http://localhost:8080 nodemon lib/app.js --exec republic --watch lib/",
6+
"start": "NODE_ENV=production API_HOST=localhost:8080 republic lib/app.js",
7+
"dev": "tsc && NODE_ENV=development API_HOST=localhost:8080 nodemon lib/app.js --exec republic --watch lib/",
88
"api": "cd api && make",
9-
"test": "API_ORIGIN=http://api jest --notify",
9+
"test": "API_HOST=api jest --notify",
1010
"clean": "rm -f {lib,pages,spec}/**/*.js"
1111
},
1212
"dependencies": {
@@ -15,12 +15,13 @@
1515
"@types/react": "^16.0",
1616
"body-parser": "^1.18.2",
1717
"express": "^4.16.2",
18-
"isomorphic-fetch": "^2.2.1",
18+
"js-yaml": "^3.10.0",
1919
"lodash.partial": "^4.2.1",
2020
"next": "^4.1.4",
2121
"react": "^16.0",
2222
"react-dom": "^16.0",
23-
"republic": "^0.4.15"
23+
"republic": "^0.4.15",
24+
"swagger-client": "^3.3.3"
2425
},
2526
"devDependencies": {
2627
"@types/enzyme": "^3.1.4",

pages/users/profile.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as React from 'react'
22
import { mountPage } from 'republic/test-next'
3-
import * as nock from 'nock'
3+
import { mockExecute } from '../../lib/api/Client'
44
import UserProfile from './profile'
55

66
const API_ORIGIN = process.env.API_ORIGIN
77

88
describe('UserProfile', () => {
99
describe('when user views the page', () => {
1010
test('user can see user name', async () => {
11-
nock(API_ORIGIN).get('/api/users/guid').reply(200, { name: 'Luke' })
11+
mockExecute('findUserById', { userId: 'guid' }).reply(200, { name: 'Luke' })
1212
const page = await mountPage(UserProfile, 'users#profile')
1313
expect(page).toIncludeText('Luke')
1414
})

0 commit comments

Comments
 (0)