Skip to content

Commit

Permalink
fix(adapter-fetch): Deprecate usage in Node in favor of node-http (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
offirgolan authored and jasonmit committed Dec 7, 2018
1 parent fa059a4 commit 001ccdd
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 64 deletions.
2 changes: 1 addition & 1 deletion examples/node-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "mocha './tests/*.test.js'"
},
"devDependencies": {
"@pollyjs/adapter-fetch": "*",
"@pollyjs/adapter-node-http": "*",
"@pollyjs/core": "*",
"@pollyjs/persister-fs": "*",
"chai": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,112 +8,146 @@
},
"entries": [
{
"_id": "ffbc4836d419fc265c3b85cbe1b7f22e",
"_id": "41b7d6ef74440e3b785f1634743f9139",
"_order": 0,
"cache": {},
"request": {
"bodySize": 0,
"cookies": [],
"headers": [],
"headersSize": 63,
"headers": [
{
"name": "accept",
"value": [
"*/*"
]
},
{
"name": "user-agent",
"value": [
"node-fetch/1.0 (+https://github.com/bitinn/node-fetch)"
]
},
{
"name": "accept-encoding",
"value": [
"gzip,deflate"
]
},
{
"name": "connection",
"value": [
"close"
]
},
{
"name": "host",
"value": "jsonplaceholder.typicode.com"
}
],
"headersSize": 228,
"httpVersion": "HTTP/1.1",
"method": "GET",
"postData": {
"mimeType": "text/plain"
},
"queryString": [],
"url": "https://jsonplaceholder.typicode.com/posts/1"
},
"response": {
"bodySize": 292,
"bodySize": 408,
"content": {
"mimeType": "application/json; charset=utf-8",
"size": 292,
"text": "{\n \"userId\": 1,\n \"id\": 1,\n \"title\": \"sunt aut facere repellat provident occaecati excepturi optio reprehenderit\",\n \"body\": \"quia et suscipit\\nsuscipit recusandae consequuntur expedita et cum\\nreprehenderit molestiae ut ut quas totam\\nnostrum rerum est autem sunt rem eveniet architecto\"\n}"
"size": 408,
"text": "[\"1f8b0800000000000003558f4b6a04410c43f7730a51eb6cb29d1be40ebd715c0a63e8fa8ccb1e2684dc3d744308012324901fe8eb02945cf4b75aae787d39a2fdd9b0d859ae282b7b4032f0214a279c93fb2e81e9e361953d3054852a61e0533923dd3066d838cace1b7ba55b9413fc3eeae7c1bda7091858b9d4a6c5d67f1d9c9a4b7a1542475fbc67f648079f93d5e27cd36c5bff87471b3b57981019c7dd53166284b4adf7b1c2b3c17928d739890de73a67031fecc680b8de2ca831cae5fb0715c693e824010000\"]"
},
"cookies": [
{
"domain": ".typicode.com",
"expires": "2019-09-20T01:24:25.000Z",
"expires": "2019-12-06T20:53:26.000Z",
"httpOnly": true,
"name": "__cfduid",
"path": "/",
"value": "dac6528c2abd0fb8d5b1028aaa7e541f31537406665"
"value": "d9a2a8da0fc8bad42e47cee6b6782bb811544129606"
}
],
"headers": [
{
"name": "access-control-allow-credentials",
"value": "true"
},
{
"name": "cache-control",
"value": "public, max-age=14400"
"name": "date",
"value": "Thu, 06 Dec 2018 20:53:26 GMT"
},
{
"name": "cf-cache-status",
"value": "HIT"
"name": "content-type",
"value": "application/json; charset=utf-8"
},
{
"name": "cf-ray",
"value": "45d07e0bfc1428be-SJC"
"name": "transfer-encoding",
"value": "chunked"
},
{
"name": "connection",
"value": "close"
},
{
"name": "content-encoding",
"value": "gzip"
"name": "set-cookie",
"value": [
"__cfduid=d9a2a8da0fc8bad42e47cee6b6782bb811544129606; expires=Fri, 06-Dec-19 20:53:26 GMT; path=/; domain=.typicode.com; HttpOnly"
]
},
{
"name": "content-type",
"value": "application/json; charset=utf-8"
"name": "x-powered-by",
"value": "Express"
},
{
"name": "date",
"value": "Thu, 20 Sep 2018 01:24:25 GMT"
"name": "vary",
"value": "Origin, Accept-Encoding"
},
{
"name": "etag",
"value": "W/\"124-yiKdLzqO5gfBrJFrcdJ8Yq0LGnU\""
"name": "access-control-allow-credentials",
"value": "true"
},
{
"name": "expect-ct",
"value": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""
"name": "cache-control",
"value": "public, max-age=14400"
},
{
"name": "pragma",
"value": "no-cache"
},
{
"name": "expires",
"value": "Thu, 20 Sep 2018 05:24:25 GMT"
"value": "Fri, 07 Dec 2018 00:53:26 GMT"
},
{
"name": "pragma",
"value": "no-cache"
"name": "x-content-type-options",
"value": "nosniff"
},
{
"name": "server",
"value": "cloudflare"
"name": "etag",
"value": "W/\"124-yiKdLzqO5gfBrJFrcdJ8Yq0LGnU\""
},
{
"name": "set-cookie",
"value": "__cfduid=dac6528c2abd0fb8d5b1028aaa7e541f31537406665; expires=Fri, 20-Sep-19 01:24:25 GMT; path=/; domain=.typicode.com; HttpOnly"
"name": "via",
"value": "1.1 vegur"
},
{
"name": "transfer-encoding",
"value": "chunked"
"name": "cf-cache-status",
"value": "HIT"
},
{
"name": "vary",
"value": "Origin, Accept-Encoding"
"name": "expect-ct",
"value": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""
},
{
"name": "via",
"value": "1.1 vegur"
"name": "server",
"value": "cloudflare"
},
{
"name": "x-content-type-options",
"value": "nosniff"
"name": "cf-ray",
"value": "4851a4585a759354-SJC"
},
{
"name": "x-powered-by",
"value": "Express"
"name": "content-encoding",
"value": "gzip"
}
],
"headersSize": 754,
Expand All @@ -122,16 +156,16 @@
"status": 200,
"statusText": "OK"
},
"startedDateTime": "2018-09-20T01:24:25.516Z",
"time": 69,
"startedDateTime": "2018-12-06T20:53:21.087Z",
"time": 130,
"timings": {
"blocked": -1,
"connect": -1,
"dns": -1,
"receive": 0,
"send": 0,
"ssl": -1,
"wait": 69
"wait": 130
}
}
],
Expand Down
18 changes: 4 additions & 14 deletions examples/node-fetch/tests/node-fetch.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
/* Start - Setup Global Fetch*/
(function() {
const { default: fetch, Response, Request, Headers } = require('node-fetch');

global.fetch = fetch;
global.Request = Request;
global.Response = Response;
global.Headers = Headers;
})();
/* End - Setup Global Fetch*/

const path = require('path');

const FetchAdapter = require('@pollyjs/adapter-fetch');
const NodeHttpAdapter = require('@pollyjs/adapter-node-http');
const FSPersister = require('@pollyjs/persister-fs');
const fetch = require('node-fetch');
const { Polly, setupMocha: setupPolly } = require('@pollyjs/core');
const { expect } = require('chai');

Polly.register(FetchAdapter);
Polly.register(NodeHttpAdapter);
Polly.register(FSPersister);

describe('node-fetch', function() {
setupPolly({
adapters: ['fetch'],
adapters: ['node-http'],
persister: 'fs',
persisterOptions: {
fs: {
Expand Down
3 changes: 2 additions & 1 deletion packages/@pollyjs/adapter-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
},
"dependencies": {
"@pollyjs/adapter": "^1.3.2",
"@pollyjs/utils": "^1.3.2"
"@pollyjs/utils": "^1.3.2",
"detect-node": "^2.0.4"
},
"devDependencies": {
"@pollyjs/core": "^1.3.2",
Expand Down
7 changes: 7 additions & 0 deletions packages/@pollyjs/adapter-fetch/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Adapter from '@pollyjs/adapter';
import isNode from 'detect-node';

import serializeHeaders from './utils/serializer-headers';

Expand All @@ -19,6 +20,12 @@ export default class FetchAdapter extends Adapter {
onConnect() {
const { context } = this.options;

if (isNode) {
console.warn(
'[Polly] [adapter:fetch] Using the fetch adapter in Node has been deprecated. Please use the node-http adapter instead.'

This comment has been minimized.

Copy link
@tncbbthositg

tncbbthositg Nov 10, 2023

Is this still valid since fetch is supported as of Node 18?

);
}

this.assert('Fetch global not found.', !!(context && context.fetch));
this.assert('Response global not found.', !!(context && context.Response));
this.assert(
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4690,6 +4690,11 @@ detect-newline@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-2.1.0.tgz#f41f1c10be4b00e87b5f13da680759f2c5bfd3e2"

detect-node@^2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/detect-node/-/detect-node-2.0.4.tgz#014ee8f8f669c5c58023da64b8179c083a28c46c"
integrity sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==

dezalgo@^1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/dezalgo/-/dezalgo-1.0.3.tgz#7f742de066fc748bc8db820569dddce49bf0d456"
Expand Down

0 comments on commit 001ccdd

Please sign in to comment.