Skip to content

Commit

Permalink
Accept a rootUrl in rpc endpoints. fix #41.
Browse files Browse the repository at this point in the history
  • Loading branch information
geclos committed May 10, 2019
1 parent 26f1397 commit b2aacff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
16 changes: 13 additions & 3 deletions __tests__/Base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe(Base, () => {
})
})

describe('rpc(label, endpoint, options)', () => {
describe('rpc(endpoint, options, label)', () => {
let request
let spy

Expand All @@ -132,17 +132,27 @@ describe(Base, () => {
})

it('sends a request using the endpoint suffix', () => {
expect(spy.mock.calls[0][0]).toBe('/api/search')
expect(spy.mock.calls.pop()[0]).toBe('/api/search')
})

it('passes the options to the api adapter', () => {
expect(spy.mock.calls[0][1]).toEqual({
expect(spy.mock.calls.pop()[1]).toEqual({
method: 'GET'
})
})

it('tracks the request with the specified labels', () => {
expect(model.isRequest('searching')).toBe(true)
})

describe('rpc with rootUrl', () => {
beforeEach(() => {
model.rpc({ rootUrl: '/another_api/search' }, { method: 'GET' }, 'searching')
})

it('should fetch with the rootUrl', () => {
expect(spy.mock.calls.pop()[0]).toBe('/another_api/search')
})
})
})
})
11 changes: 5 additions & 6 deletions src/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { action, observable, IObservableArray } from 'mobx'
import apiClient from './apiClient'
import Request from './Request'
import ErrorObject from './ErrorObject'
import { includes } from 'lodash'
import { includes, isObject } from 'lodash'

export default class Base {
@observable request: ?Request
Expand Down Expand Up @@ -68,12 +68,11 @@ export default class Base {
* non-REST endpoints that you may have in
* your API.
*/
// TODO: Type endpoint with string | { rootUrl: string }
@action
rpc (endpoint: string, options?: {}, label?: string = 'fetching'): Request {
const { promise, abort } = apiClient().post(
`${this.url()}/${endpoint}`,
options
)
rpc (endpoint: any, options?: {}, label?: string = 'fetching'): Request {
const url = isObject(endpoint) ? endpoint.rootUrl : `${this.url()}/${endpoint}`
const { promise, abort } = apiClient().post(url, options)

return this.withRequest(label, promise, abort)
}
Expand Down

0 comments on commit b2aacff

Please sign in to comment.