Skip to content

Commit

Permalink
Removed fetch default implementation (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
petruki committed May 9, 2024
1 parent 5cc128b commit 67c32e5
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 58 deletions.
27 changes: 0 additions & 27 deletions .github/workflows/publish-jsr.yml

This file was deleted.

33 changes: 33 additions & 0 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Staging CI
run-name: Staging - Node ${{ github.event.inputs.node }} / ${{ github.event.inputs.os }} by @${{ github.actor }}

on:
workflow_dispatch:
inputs:
node:
description: 'Node version'
required: true
default: '20.x'
os:
description: 'Operating System (ubuntu-20.04, ubuntu-latest, windows-latest)'
required: true
default: 'ubuntu-latest'

jobs:
test:
name: Test - Node ${{ github.event.inputs.node }} on ${{ github.event.inputs.os }}
runs-on: ${{ github.event.inputs.os }}

steps:
- name: Git checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: ${{ github.event.inputs.node }}

- run: npm install
- run: npm test
2 changes: 1 addition & 1 deletion .nycrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"all": true,
"exclude": ["test", "src/lib/utils/timed-match/match-proc.js", "src/lib/utils/fetchFacade.js"],
"exclude": ["test", "src/lib/utils/timed-match/match-proc.js"],
"output": "reports",
"reporter" : [
"html",
Expand Down
19 changes: 0 additions & 19 deletions jsr.json

This file was deleted.

2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.sources=src
sonar.tests=test
sonar.language=js
sonar.exclusions=src/**/*.d.ts, src/lib/utils/timed-match/match-proc.js, src/lib/utils/fetchFacade.js
sonar.exclusions=src/**/*.d.ts, src/lib/utils/timed-match/match-proc.js

sonar.dynamicAnalysis=reuseReports
# Encoding of the source code. Default is default system encoding
Expand Down
1 change: 0 additions & 1 deletion src/declarations.d.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class Switcher {
}

static #buildOptions(options) {
remote.removeAgent();
if (SWITCHER_OPTIONS.CERT_PATH in options && options.certPath) {
remote.setCerts(options.certPath);
}
Expand Down
8 changes: 6 additions & 2 deletions src/lib/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export function setCerts(certPath) {
});
}

export function removeAgent() {
httpClient = undefined;
}

export function getEntry(input) {
if (!input) {
return undefined;
Expand Down Expand Up @@ -113,7 +117,7 @@ export async function checkSwitchers(url, token, switcherKeys) {
throw new Error(`[checkSwitchers] failed with status ${response.status}`);
}

const json = await response.json();
const json = response.json();
if (json.not_found.length)
throw new CheckSwitcherError(json.not_found);
} catch (e) {
Expand Down Expand Up @@ -164,7 +168,7 @@ export async function resolveSnapshot(url, token, domain, environment, component
});

if (response.status == 200) {
return JSON.stringify(await response.json(), null, 4);
return JSON.stringify(response.json(), null, 4);
}

throw new Error(`[resolveSnapshot] failed with status ${response.status}`);
Expand Down
21 changes: 20 additions & 1 deletion src/lib/utils/fetchFacade.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import https from 'node:https';
import http from 'node:http';

export default class FetchFacade {
static async fetch(url, init) {
return fetch(url, init);
return new Promise((resolve, reject) => {
const lib = url.startsWith('https') ? https : http;
const request = lib.request(url, init, (response) => {
let body = '';
response.on('data', (chunk) => body += chunk);
response.on('end', () => FetchFacade.#handleResponse(response, body, resolve));
});
request.on('error', reject);
if (init.body) {
request.write(init.body);
}
request.end();
});
}

static #handleResponse(response, body, resolve) {
resolve({ status: response.statusCode, json: () => JSON.parse(body) });
}
}
25 changes: 25 additions & 0 deletions test/switcher-integrated.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { assert } from 'chai';

import { Switcher } from '../switcher-client.js';

describe('Switcher integrated test', () => {

it('should hit remote API and return Switcher response', async function () {
this.timeout(3000);

// given context build
Switcher.buildContext({
url: 'https://api.switcherapi.com',
apiKey: 'JDJiJDA4JEFweTZjSTR2bE9pUjNJOUYvRy9raC4vRS80Q2tzUnk1d3o1aXFmS2o5eWJmVW11cjR0ODNT',
domain: 'Playground',
component: 'switcher-playground'
});

// test
const switcher = Switcher.factory().detail();
const result = await switcher.isItOn('CLIENT_JS_FEATURE_1');

assert.isNotNull(result);
});

});
6 changes: 0 additions & 6 deletions tsconfig.json

This file was deleted.

0 comments on commit 67c32e5

Please sign in to comment.