From 6a69ac28b9f732856467db3017b90c55dc9248d3 Mon Sep 17 00:00:00 2001 From: Marcelo Lauxen Date: Thu, 3 Jun 2021 20:49:13 -0300 Subject: [PATCH] Add prefix Fetch to Request and Response classes As pointed in https://github.com/rails/request.js/issues/3 the actual Request/Response classes clashes with the built-in classes from JS --- README.md | 4 ++-- src/{request.js => fetch_request.js} | 6 +++--- src/{response.js => fetch_response.js} | 2 +- src/index.js | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) rename src/{request.js => fetch_request.js} (93%) rename src/{response.js => fetch_response.js} (98%) diff --git a/README.md b/README.md index a3e9827..a598871 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 diff --git a/src/request.js b/src/fetch_request.js similarity index 93% rename from src/request.js rename to src/fetch_request.js index 1f3283b..585990a 100644 --- a/src/request.js +++ b/src/fetch_request.js @@ -1,7 +1,7 @@ -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 @@ -9,7 +9,7 @@ export class Request { } 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 { diff --git a/src/response.js b/src/fetch_response.js similarity index 98% rename from src/response.js rename to src/fetch_response.js index 47280c8..2a84893 100644 --- a/src/response.js +++ b/src/fetch_response.js @@ -1,4 +1,4 @@ -export class Response { +export class FetchResponse { constructor (response) { this.response = response } diff --git a/src/index.js b/src/index.js index b9b9389..9505ac1 100644 --- a/src/index.js +++ b/src/index.js @@ -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 } \ No newline at end of file +export { FetchRequest, FetchResponse } \ No newline at end of file