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

Add prefix Fetch to the Request and Response classes #8

Merged
merged 1 commit into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ yarn add @rails/request.js

# How to use

Just import the `Request` class from the package and instantiate it passing the request `method`, `url`, `options`, then call `await request.perform()` and do what do you need with the response.
Just import the `FetchRequest` class from the package and instantiate it passing the request `method`, `url`, `options`, then call `await request.perform()` and do what do you need with the response.

Example:

Expand All @@ -25,7 +25,7 @@ import { Request } from '@rails/request.js'
....

async myMethod () {
const request = new Request('post', 'localhost:3000/my_endpoint', { body: { name: 'Request.JS' }})
const request = new FetchRequest('post', 'localhost:3000/my_endpoint', { body: { name: 'Request.JS' }})
const response = await request.perform()
if (response.ok) {
const body = await response.text
Expand Down
6 changes: 3 additions & 3 deletions src/request.js → src/fetch_request.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Response } from './response'
import { FetchResponse } from './fetch_response'
import { getCookie } from './lib/cookie'

export class Request {
export class FetchRequest {
constructor (method, url, options = {}) {
this.method = method
this.url = url
this.options = options
}

async perform () {
const response = new Response(await window.fetch(this.url, this.fetchOptions))
const response = new FetchResponse(await window.fetch(this.url, this.fetchOptions))
if (response.unauthenticated && response.authenticationURL) {
return Promise.reject(window.location.href = response.authenticationURL)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/response.js → src/fetch_response.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class Response {
export class FetchResponse {
constructor (response) {
this.response = response
}
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Request } from './request'
import { Response } from './response'
import { FetchRequest } from './fetch_request'
import { FetchResponse } from './fetch_response'

export { Request, Response }
export { FetchRequest, FetchResponse }