Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Navigator] Return new navigators such that they can be reused #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 39 additions & 48 deletions src/Navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,19 @@ class Navigator {
}

static resume (location, resource, options = {}) {
return new Navigator(options)
._setLocation(location)
._setResource(resource)
return new Navigator(options, { resource, location })
}

constructor (options, { status, location, response, resource } = {}) {
this.options = {
...Navigator.defaultOptions,
...options
}

this._status = status
this._response = response
this._resource = resource
this._location = location
}

_setResource (resource) {
Expand All @@ -33,13 +43,6 @@ class Navigator {
return this
}

constructor (options) {
this.options = {
...Navigator.defaultOptions,
...options
}
}

location () {
return this._location
}
Expand All @@ -57,7 +60,7 @@ class Navigator {
}

resolveLink (rel, params) {
const relativeHref = this._resource.getHref(rel)
const relativeHref = this.resource().getHref(rel)

if (!relativeHref) {
throw new Error(`Attempting to follow the link '${rel}', which does not exist`)
Expand All @@ -66,7 +69,7 @@ class Navigator {
const { href, params: queryParams } = resolveLink(relativeHref, params)

return {
href: makeAbsolute(this._location, href),
href: makeAbsolute(this.location(), href),
queryParams
}
}
Expand Down Expand Up @@ -104,12 +107,7 @@ class Navigator {
response
} = await this.options.get(url, params, config)

this._status = status
this._location = location
this._response = response
this._resource = Resource.fromObject(body)

return this
return new Navigator(this.options, { status, location, response, resource: Resource.fromObject(body) })
}

async postUrl (url, body, config) {
Expand All @@ -120,16 +118,16 @@ class Navigator {
response
} = await this.options.post(url, body, config)

this._status = status
this._location = location
this._response = response
this._resource = Resource.fromObject(responseBody)

if (this.options.followRedirects && status === 201) {
return this.followRedirect(config)
return new Navigator(this.options, {
status,
location,
response,
resource: Resource.fromObject(responseBody)
}).followRedirect(config)
}

return this
return new Navigator(this.options, { status, location, response, resource: Resource.fromObject(responseBody) })
}

async putUrl (url, body, config) {
Expand All @@ -140,16 +138,16 @@ class Navigator {
response
} = await this.options.put(url, body, config)

this._status = status
this._location = location
this._response = response
this._resource = Resource.fromObject(responseBody)

if (this.options.followRedirects && status === 201) {
return this.followRedirect(config)
return new Navigator(this.options, {
status,
location,
response,
resource: Resource.fromObject(responseBody)
}).followRedirect(config)
}

return this
return new Navigator(this.options, { status, location, response, resource: Resource.fromObject(responseBody) })
}

async patchUrl (url, body, config) {
Expand All @@ -160,16 +158,16 @@ class Navigator {
response
} = await this.options.patch(url, body, config)

this._status = status
this._location = location
this._response = response
this._resource = Resource.fromObject(responseBody)

if (this.options.followRedirects && status === 204) {
return this.followRedirect(config)
return new Navigator(this.options, {
status,
location,
response,
resource: Resource.fromObject(responseBody)
}).followRedirect(config)
}

return this
return new Navigator(this.options, { status, location, response, resource: Resource.fromObject(responseBody) })
}

async deleteUrl (url, body, config) {
Expand All @@ -180,18 +178,11 @@ class Navigator {
response
} = await this.options.delete(url, body, config)

this._status = status
this._location = location
this._response = response
this._resource = Resource.fromObject(responseBody)

return this
return new Navigator(this.options, { status, location, response, resource: Resource.fromObject(responseBody) })
}

async followRedirect (config) {
const fullLocation = makeAbsolute(
this._location, this.getHeader('location'))
return this.getUrl(fullLocation, {}, config)
return this.getUrl(makeAbsolute(this.location(), this.getHeader('location')), {}, config)
}
}

Expand Down