Skip to content

Commit

Permalink
feat: added @dereekb/util/fetch
Browse files Browse the repository at this point in the history
- added fetch api utility library for building a ConfiguredFetch
  • Loading branch information
dereekb committed Nov 7, 2022
1 parent 5399706 commit 6afa4a4
Show file tree
Hide file tree
Showing 23 changed files with 409 additions and 23 deletions.
7 changes: 6 additions & 1 deletion NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
Set of notes relevant to setting up an Nx environment, similar to this one.

# Using Nx
## Creating a NodeJs Library
## Creating a NodeJS Library
Be sure to include the `--buildable` and `--publishable` flags if relevant.

Example: `nx g @nrwl/node:library firebase --buildable --publishable --importPath @dereekb/firebase`

### Creating a child NodeJS Library
Example: `nx generate @nrwl/node:library --name=fetch --buildable --publishable --importPath @dereekb/util/fetch --directory=util`

This example will end up as a child of @dereekb/util.

## Creating an Angular Library

Example: `nx generate @nrwl/angular:library --name=dbx-firebase --buildable --publishable --importPath @dereekb/dbx-firebase`
Expand Down
63 changes: 56 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@
"@types/mapbox__geo-viewport": "^0.4.1",
"@types/mapbox-gl": "^2.7.5",
"@types/mockdate": "^3.0.0",
"@types/node": "^16.11.7",
"@types/node": "~18.11.9",
"@types/node-fetch": "^2.6.2",
"@types/segment-analytics": "^0.0.34",
"@typescript-eslint/eslint-plugin": "5.33.0",
"@typescript-eslint/parser": "5.33.0",
Expand All @@ -141,6 +142,7 @@
"jest-preset-angular": "12.2.0",
"mockdate": "^3.0.5",
"ng-packagr": "~14.1.0",
"node-fetch": "^2.6.7",
"nx": "14.5.10",
"postcss": "^8.4.7",
"postcss-import": "^14.0.2",
Expand Down
3 changes: 3 additions & 0 deletions packages/util/fetch/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [["@nrwl/web/babel", { "useBuiltIns": "usage" }]]
}
18 changes: 18 additions & 0 deletions packages/util/fetch/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
19 changes: 19 additions & 0 deletions packages/util/fetch/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-disable */
(global as any).testFolderRootPath = '<rootDir>/../../..';
(global as any).appTestType = 'node';

module.exports = {
displayName: 'util-fetch',
preset: '../../../jest.preset.ts',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json'
}
},
testEnvironment: 'node',
transform: {
'^.+\\.[tj]sx?$': 'ts-jest'
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../../coverage/packages/util/fetch'
};
4 changes: 4 additions & 0 deletions packages/util/fetch/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@dereekb/util/fetch",
"version": "0.0.1"
}
34 changes: 34 additions & 0 deletions packages/util/fetch/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/util/fetch/src",
"projectType": "library",
"targets": {
"build-base": {
"executor": "@nrwl/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/util/fetch",
"tsConfig": "packages/util/fetch/tsconfig.lib.json",
"packageJson": "packages/util/fetch/package.json",
"main": "packages/util/fetch/src/index.ts",
"assets": ["packages/util/fetch/*.md"]
}
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/util/fetch/**/*.ts"]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["coverage/packages/util/fetch", "./.reports/jest/util-fetch.junit.xml"],
"options": {
"jestConfig": "packages/util/fetch/jest.config.ts",
"passWithNoTests": true
}
}
},
"tags": []
}
1 change: 1 addition & 0 deletions packages/util/fetch/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib';
60 changes: 60 additions & 0 deletions packages/util/fetch/src/lib/fetch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { itShouldFail, expectFail } from '@dereekb/util/test';
import { fetchService, fetchRequestFactory, FetchRequestFactory, FetchService } from './fetch';
import fetch, { Request, RequestInfo, RequestInit } from 'node-fetch';

// TEMP: Fetch global is not available in jest? Use node-fetch@2 for now.

const testFetch: FetchService = fetchService({
makeFetch: fetch as any,
makeRequest: (x, y) => new Request(x as RequestInfo, y as RequestInit) as any
});

describe('fetchRequestFactory()', () => {
describe('function', () => {
describe('with baseUrl', () => {
describe('invalid', () => {
itShouldFail('when creating a factory with an invalid base url', () => {
const baseUrl = 'invalidurl';

expectFail(() =>
testFetch.fetchRequestFactory({
baseUrl
})
);
});
});

describe('valid', () => {
const baseUrl = 'https://components.dereekb.com/';

const factory = testFetch.fetchRequestFactory({
baseUrl
});

it('should retain the path of an input request.', () => {
const expectedUrl = 'https://google.com/';
const request = testFetch.makeRequest(expectedUrl);
const result = factory(request);
expect(result.url).toBe(expectedUrl);
});

it('should retain the path of an input URL.', () => {
const expectedUrl = 'https://google.com/';
const request = new URL(expectedUrl);
const result = factory(request);
expect(result.url).toBe(expectedUrl);
});

it('should append the base url to the request.', () => {
const result = factory('test');
expect(result.url).toBe('https://components.dereekb.com/test');
});

it('should append the base url to the request if it has a front slash.', () => {
const result = factory('/test');
expect(result.url).toBe('https://components.dereekb.com/test');
});
});
});
});
});
Loading

0 comments on commit 6afa4a4

Please sign in to comment.