Skip to content

Commit

Permalink
Remove request APIs since Node.js now has fetch()
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Aug 25, 2024
1 parent 5b26c6f commit 7b2a418
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 287 deletions.
10 changes: 1 addition & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@ export { ALLOWED_META } from './allowed-meta/index.js'
export { BaseServer, wasNot403 } from './base-server/index.js'
export { Context } from './context/index.js'
export { filterMeta } from './filter-meta/index.js'
export {
del,
get,
patch,
post,
put,
request,
ResponseError
} from './request/index.js'
export { ResponseError } from './request/index.js'
export { Server } from './server/index.js'
export { TestClient } from './test-client/index.js'
export { TestServer } from './test-server/index.js'
100 changes: 1 addition & 99 deletions request/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import type { RequestInit } from 'node-fetch'
import type fetch from 'node-fetch'

/**
* Throwing this error in `accessAndProcess` or `accessAndLoad`
* will deny the action.
Expand All @@ -9,100 +6,5 @@ export class ResponseError extends Error {
name: 'ResponseError'
statusCode: number

constructor(
statusCode: number,
url: string,
opts: RequestInit,
response: string
)
constructor(statusCode: number, url: string)
}

/**
* Syntax sugar around `node-fetch`, which throws {@link ResponseError}
* on non-2xx response.
*
* @param url Resource URL.
* @param opts `fetch()` options.
* @param fetcher A way to replace `fetch()` for tests.
*/
export function request<Body = any>(
url: string,
opts?: RequestInit,
fetcher?: typeof fetch
): Body

/**
* Syntax sugar for `GET` requests by `node-fetch`, throwing
* {@link ResponseError} on non-2xx response and parsing JSON response.
*
* ```js
* import { get } from '@logux/server'
*
* let user = get(url)
* ```
*
* @param url Resource URL.
* @param opts `fetch()` options.
* @param fetcher A way to replace `fetch()` for tests.
*/
export function get<Body = any>(
url: string,
opts?: Omit<RequestInit, 'method'>,
fetcher?: typeof fetch
): Body

/**
* Syntax sugar for `POST` requests by `node-fetch`, throwing
* {@link ResponseError} on non-2xx response.
*
* @param url Resource URL.
* @param opts `fetch()` options.
* @param fetcher A way to replace `fetch()` for tests.
*/
export function post<Body = any>(
url: string,
opts?: Omit<RequestInit, 'method'>,
fetcher?: typeof fetch
): Body

/**
* Syntax sugar for `PUT` requests by `node-fetch`, throwing
* {@link ResponseError} on non-2xx response and parsing JSON response.
*
* @param url Resource URL.
* @param opts `fetch()` options.
* @param fetcher A way to replace `fetch()` for tests.
*/
export function put<Body = any>(
url: string,
opts?: Omit<RequestInit, 'method'>,
fetcher?: typeof fetch
): Body

/**
* Syntax sugar for `PATCH` requests by `node-fetch`, throwing
* {@link ResponseError} on non-2xx response and parsing JSON response.
*
* @param url Resource URL.
* @param opts `fetch()` options.
* @param fetcher A way to replace `fetch()` for tests.
*/
export function patch<Body = any>(
url: string,
opts?: Omit<RequestInit, 'method'>,
fetcher?: typeof fetch
): Body

/**
* Syntax sugar for `DELETE` requests by `node-fetch`, throwing
* {@link ResponseError} on non-2xx response and parsing JSON response.
*
* @param url Resource URL.
* @param opts `fetch()` options.
* @param fetcher A way to replace `fetch()` for tests.
*/
export function del<Body = any>(
url: string,
opts?: Omit<RequestInit, 'method'>,
fetcher?: typeof fetch
): Body
42 changes: 2 additions & 40 deletions request/index.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,7 @@
import fetch from 'node-fetch'

function argsToString(url, opts, response) {
let { method, ...other } = opts
return `${method || 'GET'} ${url} ${JSON.stringify(other)} ${response}`
}

export class ResponseError extends Error {
constructor(statusCode, url, opts, response) {
super(`${statusCode} response on ${argsToString(url, opts, response)}`)
constructor(statusCode, url) {
super(`${statusCode} response on ${url}`)
this.name = 'ResponseError'
this.statusCode = statusCode
}
}

export async function request(url, opts = {}, fetcher = fetch) {
let response = await fetcher(url, opts)
if (response.status === 204) {
return undefined
} else if (response.status >= 200 && response.status <= 299) {
return response.json()
} else {
throw new ResponseError(response.status, url, opts, await response.text())
}
}

export function get(url, opts = {}, fetcher = fetch) {
return request(url, { ...opts, method: 'GET' }, fetcher)
}

export function post(url, opts = {}, fetcher = fetch) {
return request(url, { ...opts, method: 'POST' }, fetcher)
}

export function put(url, opts = {}, fetcher = fetch) {
return request(url, { ...opts, method: 'PUT' }, fetcher)
}

export function patch(url, opts = {}, fetcher = fetch) {
return request(url, { ...opts, method: 'PATCH' }, fetcher)
}

export function del(url, opts = {}, fetcher = fetch) {
return request(url, { ...opts, method: 'DELETE' }, fetcher)
}
134 changes: 0 additions & 134 deletions request/test.test.js

This file was deleted.

10 changes: 5 additions & 5 deletions server-client/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1841,8 +1841,8 @@ it('denies access on 403 error', async () => {
let app = createServer()
app.log.keepActions()

let error404 = new ResponseError(404, '/a', {}, '404')
let error403 = new ResponseError(403, '/a', {}, '403')
let error404 = new ResponseError(404, '/a')
let error403 = new ResponseError(403, '/a')
let error = new Error('test')

let catched: Error[] = []
Expand Down Expand Up @@ -1915,9 +1915,9 @@ it('undoes action with notFound on 404 error', async () => {
let app = createServer()
app.log.keepActions()

let error500 = new ResponseError(500, '/a', {}, '500')
let error404 = new ResponseError(404, '/a', {}, '404')
let error403 = new ResponseError(403, '/a', {}, '403')
let error500 = new ResponseError(500, '/a')
let error404 = new ResponseError(404, '/a')
let error403 = new ResponseError(403, '/a')
let error = new Error('test')

let catched: Error[] = []
Expand Down

0 comments on commit 7b2a418

Please sign in to comment.