Skip to content

Commit

Permalink
Merge pull request #8 from marcelolx/rename-classes
Browse files Browse the repository at this point in the history
Add prefix `Fetch` to the `Request` and `Response` classes
  • Loading branch information
marcelolx authored Jun 4, 2021
2 parents c8d7881 + 6a69ac2 commit a4ba4ac
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
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 }

0 comments on commit a4ba4ac

Please sign in to comment.