balena-sdk 16.9.1 → 16.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/es2015/index.d.ts CHANGED
@@ -1,9 +1,7 @@
1
1
  /// <reference types="memoizee" />
2
- import type { Pine as PineBase, PineStrict as PineStrictBase } from '../typings/balena-pine';
2
+ import type { Pine } from './pine';
3
3
  import type { BalenaRequest, Interceptor } from 'balena-request';
4
- import type { ResourceTypeMap } from './types/models';
5
- export declare type Pine = PineBase<ResourceTypeMap>;
6
- export declare type PineStrict = PineStrictBase<ResourceTypeMap>;
4
+ export type { Pine, PineStrict } from './pine';
7
5
  export * from './types/models';
8
6
  export * from './types/jwt';
9
7
  export * from './types/contract';
@@ -404,7 +402,10 @@ declare const sdkTemplate: {
404
402
  getAllByService: (parentParam: string | number, options?: import("../typings/pinejs-client-core").ODataOptions<import("./types/models").ServiceEnvironmentVariable> | undefined) => Promise<import("./types/models").ServiceEnvironmentVariable[]>;
405
403
  getAllByApplication(slugOrId: string | number, options?: import("../typings/pinejs-client-core").ODataOptions<import("./types/models").ServiceEnvironmentVariable>): Promise<import("./types/models").ServiceEnvironmentVariable[]>;
406
404
  get: (parentParam: string | number, key: string) => Promise<string | undefined>;
407
- set: (parentParam: string | number, key: string, value: string) => Promise<void>;
405
+ set: (parentParam: string | number, key: string, value: string) => Promise<void>; /**
406
+ * @namespace settings
407
+ * @memberof balena
408
+ */
408
409
  remove: (parentParam: string | number, key: string) => Promise<void>;
409
410
  };
410
411
  };
package/es2015/index.js CHANGED
@@ -79,7 +79,7 @@ const getSdk = function ($opts) {
79
79
  const { getRequest } = require('balena-request');
80
80
  const BalenaAuth = require('balena-auth')
81
81
  .default;
82
- const { BalenaPine } = require('balena-pine');
82
+ const { createPinejsClient } = require('./pine');
83
83
  const errors = require('balena-errors');
84
84
  const { PubSub } = require('./util/pubsub');
85
85
  /**
@@ -121,7 +121,7 @@ const getSdk = function ($opts) {
121
121
  }
122
122
  const auth = new BalenaAuth(opts);
123
123
  const request = getRequest(Object.assign(Object.assign({}, opts), { auth }));
124
- const pine = new BalenaPine({}, Object.assign(Object.assign({}, opts), { auth, request }));
124
+ const pine = createPinejsClient({}, Object.assign(Object.assign({}, opts), { auth, request }));
125
125
  const pubsub = new PubSub();
126
126
  const sdk = {};
127
127
  const deps = {
@@ -239,7 +239,7 @@ const getSdk = function ($opts) {
239
239
  * @memberof balena
240
240
  *
241
241
  * @description
242
- * The balena-pine instance used internally. This should not be necessary
242
+ * The pinejs-client instance used internally. This should not be necessary
243
243
  * in normal usage, but can be useful if you want to directly make pine
244
244
  * queries to the api for some resource that isn't directly supported
245
245
  * in the SDK.
@@ -0,0 +1,22 @@
1
+ import { AnyObject, Params } from 'pinejs-client-core';
2
+ import type * as PineClient from '../typings/pinejs-client-core';
3
+ import type { ResourceTypeMap } from './types/models';
4
+ interface BackendParams {
5
+ apiUrl: string;
6
+ apiVersion: string;
7
+ apiKey?: string;
8
+ request: {
9
+ send: (options: AnyObject) => Promise<{
10
+ body: any;
11
+ }>;
12
+ };
13
+ auth: import('balena-auth').default;
14
+ }
15
+ export declare type Pine = PineClient.Pine<ResourceTypeMap>;
16
+ /**
17
+ * A variant that makes $select mandatory, helping to create
18
+ * requests that explicitly fetch only what your code needs.
19
+ */
20
+ export declare type PineStrict = PineClient.PineStrict<ResourceTypeMap>;
21
+ export declare const createPinejsClient: (params: Params, backendParams: BackendParams) => Pine;
22
+ export {};
package/es2015/pine.js ADDED
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createPinejsClient = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const url = require("url");
6
+ const errors = require("balena-errors");
7
+ const pinejs_client_core_1 = require("pinejs-client-core");
8
+ /**
9
+ * @class
10
+ * @classdesc A PineJS Client subclass to communicate with balena.
11
+ * @private
12
+ *
13
+ * @description
14
+ * This subclass makes use of the [balena-request](https://github.com/balena-io-modules/balena-request) project.
15
+ */
16
+ class PinejsClient extends pinejs_client_core_1.PinejsClientCore {
17
+ constructor(params, backendParams) {
18
+ super(Object.assign(Object.assign({}, params), { apiPrefix: url.resolve(backendParams.apiUrl, `/${backendParams.apiVersion}/`) }));
19
+ this.backendParams = backendParams;
20
+ this.backendParams = backendParams;
21
+ this.API_URL = backendParams.apiUrl;
22
+ this.API_VERSION = backendParams.apiVersion;
23
+ }
24
+ /**
25
+ * @summary Perform a network request to balena.
26
+ * @method
27
+ * @private
28
+ *
29
+ * @param {Object} options - request options
30
+ * @returns {Promise<*>} response body
31
+ *
32
+ * @todo Implement caching support.
33
+ */
34
+ _request(options) {
35
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
36
+ const { apiKey, apiUrl, auth, request } = this.backendParams;
37
+ const hasKey = yield auth.hasKey();
38
+ const authenticated = hasKey || (apiKey != null && apiKey.length > 0);
39
+ options = Object.assign({ apiKey, baseUrl: apiUrl, sendToken: authenticated && !options.anonymous }, options);
40
+ try {
41
+ const { body } = yield request.send(options);
42
+ return body;
43
+ }
44
+ catch (err) {
45
+ if (err.statusCode !== 401) {
46
+ throw err;
47
+ }
48
+ // Always return the API error when the anonymous flag is used.
49
+ if (options.anonymous) {
50
+ throw err;
51
+ }
52
+ // We want to allow unauthenticated users to make requests
53
+ // to public resources, but still reject with a NotLoggedIn
54
+ // error if the response ends up being a 401.
55
+ if (!authenticated) {
56
+ throw new errors.BalenaNotLoggedIn();
57
+ }
58
+ throw err;
59
+ }
60
+ });
61
+ }
62
+ }
63
+ const createPinejsClient = (...args) => {
64
+ const pine = new PinejsClient(...args);
65
+ // @ts-expect-error
66
+ return pine;
67
+ };
68
+ exports.createPinejsClient = createPinejsClient;
@@ -6,5 +6,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  // being embedded in the dist of the consumer project
7
7
  // which we want to avoid, both b/c of the dist size increase &
8
8
  // the security concerns of including the versions of the dependencies
9
- const sdkVersion = '16.9.1';
9
+ const sdkVersion = '16.9.2';
10
10
  exports.default = sdkVersion;