Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "0.2.1",
"description": "This module provides the nodejs interface for the pinejs API using request.",
"main": "build/index.js",
"browser": "build/index.umd.js",
"types": "build/index.d.ts",
"repository": {
"type": "git",
Expand All @@ -24,7 +23,7 @@
"build/"
],
"scripts": {
"build": "tsc && rollup -c",
"build": "tsc",
"lint": "balena-lint --typescript src tests",
"lint-fix": "balena-lint --typescript --fix src tests",
"test:node": "mocha -r ts-node/register --reporter spec tests/**/*.spec.ts",
Expand All @@ -34,24 +33,26 @@
},
"devDependencies": {
"@balena/lint": "^5.0.4",
"@rollup/plugin-commonjs": "^11.1.0",
"@rollup/plugin-node-resolve": "^7.1.3",
"@types/chai": "^4.2.11",
"@types/chai-as-promised": "^7.1.2",
"@types/mocha": "^7.0.2",
"balena-config-karma": "^2.3.1",
"balena-auth": "^4.0.2",
"balena-config-karma": "^3.0.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"fetch-ponyfill": "^6.1.0",
"husky": "^4.2.5",
"karma": "^5.0.9",
"karma-chrome-launcher": "^2.2.0",
"lint-staged": "^10.1.7",
"mocha": "^7.2.0",
"rollup": "^2.10.0",
"ts-node": "^8.10.1",
"typescript": "^3.9.3"
"typescript": "^4.0.3"
},
"dependencies": {
"pinejs-client-core": "^5.7.0"
"pinejs-client-core": "^6.9.1"
},
"peerDependencies": {
"balena-auth": "^4.0.2"
}
}
15 changes: 0 additions & 15 deletions rollup.config.js

This file was deleted.

34 changes: 19 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { PinejsClientCoreFactory } from 'pinejs-client-core';
export type { PinejsClientCoreFactory } from 'pinejs-client-core';
import { AnyObject, Params, PinejsClientCore } from 'pinejs-client-core';
import type BalenaAuth from 'balena-auth';
export * as PinejsClient from 'pinejs-client-core';

interface BackendParams {
/** The browser fetch API implementation or a compatible one */
fetch?: typeof fetch;
auth?: BalenaAuth;
}

type PromiseObj = Promise<{}>;

export class RequestError extends Error {
public code = 'PineClientFetchRequestError';

Expand All @@ -20,15 +20,8 @@ export class RequestError extends Error {
}
}

export default class PineFetch extends PinejsClientCoreFactory(Promise)<
PineFetch,
PromiseObj,
Promise<PinejsClientCoreFactory.PromiseResultTypes>
> {
constructor(
params: PinejsClientCoreFactory.Params,
public backendParams: BackendParams,
) {
export default class PineFetch extends PinejsClientCore<PineFetch> {
constructor(params: Params, public backendParams: BackendParams) {
super(params);
if (
typeof backendParams?.fetch !== 'function' &&
Expand All @@ -46,8 +39,8 @@ export default class PineFetch extends PinejsClientCoreFactory(Promise)<
...options
}: {
url: string;
body?: PinejsClientCoreFactory.AnyObject;
} & PinejsClientCoreFactory.AnyObject) {
body?: AnyObject;
} & AnyObject) {
const normalizedBody =
body == null
? null
Expand All @@ -58,6 +51,17 @@ export default class PineFetch extends PinejsClientCoreFactory(Promise)<
// Assign to a variable first, otherwise browser fetch errors in case the context is different.
const fetchImplementation = this.backendParams?.fetch ?? fetch;

const { auth } = this.backendParams;
if (
!options.headers?.authorization &&
auth != null &&
(await auth.hasKey())
) {
const key = await auth.getKey();
options.headers ??= {};
options.headers.authorization = `Bearer ${key}`;
}

const response = await fetchImplementation(url, {
...options,
body: normalizedBody,
Expand Down
10 changes: 2 additions & 8 deletions tests/setup.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import * as fetchPonyfillFactory from 'fetch-ponyfill';
import PineFetchNode from '../src/index';
// tslint:disable-next-line:no-var-requires
const { default: PineFetchBrowser } = require('../build/index.umd');
import PineFetch from '../src/index';

const API_BASE_URL = 'https://api.balena-cloud.com/';
const API_VERSION = 'v5/';

const IS_BROWSER = typeof window !== 'undefined';

const PineFetch: typeof PineFetchNode = IS_BROWSER
? PineFetchBrowser
: PineFetchNode;

// While testing on a browser, rely on the library to use the native `fetch`.
const backendParams = IS_BROWSER
? {}
: { fetch: fetchPonyfillFactory({ Promise }).fetch };

export interface TestSuiteContext extends Mocha.Context {
// So that TS lets us type the `this` of `it` calls.
pineClient?: PineFetchNode;
pineClient?: PineFetch;
}

export const givenAPineClient = function (beforeFn: Mocha.HookFunction) {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"removeComments": true,
"sourceMap": true,
"strict": true,
"target": "es5",
"target": "es2015",
"lib": [
"dom"
],
Expand Down