@smapiot/piral-cloud-node 0.12.2 → 0.12.3-pre.20220810.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 smapiot
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,79 @@
1
+ [![Piral Logo](https://github.com/smapiot/piral/raw/main/docs/assets/logo.png)](https://piral.io)
2
+
3
+ # `@smapiot/piral-cloud-node`
4
+
5
+ Node-usable API Client for the [Piral Feed Service](https://www.piral.cloud).
6
+
7
+ This package is compatible with Node.js v12 or higher.
8
+
9
+ ## Important Links
10
+
11
+ * 📢 **[We are hiring!](https://smapiot.com/jobs)** - work with us on Piral, its ecosystem and our users
12
+ * 🌍 [Website](https://www.piral.cloud/) - learn more about the Piral Feed Service
13
+ * 📖 [Documentation](https://docs.piral.cloud/) - everything to get started and master the Piral Feed Service
14
+ * 👪 [Community Chat](https://gitter.im/piral-io/community) - ask questions and provide answers in our Gitter room
15
+
16
+ ## Installation
17
+
18
+ The package can be installed with your favorite npm client, e.g.:
19
+
20
+ ```sh
21
+ npm i @smapiot/piral-cloud-node
22
+ ```
23
+
24
+ Once installed the package is ready to be used.
25
+
26
+ ## Usage
27
+
28
+ To use the package you need to import the `createServiceClient` function and call it:
29
+
30
+ ```js
31
+ const { createServiceClient } = require('@smapiot/piral-cloud-node');
32
+
33
+ const client = createServiceClient({
34
+ apiKey: '123...',
35
+ });
36
+
37
+ // use the client
38
+ ```
39
+
40
+ If you want to use it with your own Piral Feed Service instance then provide the `host` option:
41
+
42
+ ```js
43
+ const { createServiceClient } = require('@smapiot/piral-cloud-node');
44
+
45
+ const client = createServiceClient({
46
+ apiKey: '123...',
47
+ host: 'http://localhost:9000',
48
+ });
49
+
50
+ // use the client
51
+ ```
52
+
53
+ ## Example
54
+
55
+ If you want to just manipulate a feed you can go ahead and use the `doUpdateFeed` function.
56
+
57
+ ```js
58
+ const { createServiceClient } = require('@smapiot/piral-cloud-node');
59
+
60
+ const client = createServiceClient({
61
+ apiKey: '123...',
62
+ });
63
+
64
+ const feedId = 'my-feed';
65
+
66
+ console.log('Before', await client.doQueryFeed(feedId));
67
+
68
+ await client.doUpdateFeed(feedId, {
69
+ contributors: ['a@b.com', 'c@d.com'],
70
+ });
71
+
72
+ console.log('After', await client.doQueryFeed(feedId));
73
+ ```
74
+
75
+ Everything is fully typed using TypeScript declarations.
76
+
77
+ ## License
78
+
79
+ This SDK is released using the MIT license. For more information see the [license file](./LICENSE).
package/lib/index.d.ts CHANGED
@@ -1,21 +1,4 @@
1
1
  declare module "@smapiot/piral-cloud-node" {
2
- /**
3
- * The options for creating a new service client.
4
- */
5
- export interface CreateServiceClientOptions {
6
- /**
7
- * Defines the host to talk to. Set this if you want to communicate
8
- * with your own feed service instance.
9
- * @default https://feed.piral.cloud
10
- */
11
- host?: string;
12
- /**
13
- * Defines the API key to use for making the HTTP calls. Needs
14
- * to be provided.
15
- */
16
- apiKey: string;
17
- }
18
-
19
2
  /**
20
3
  * Creates a new service client for use in Node.js-based applications.
21
4
  * @param options The options for creating the client.
@@ -23,6 +6,11 @@ declare module "@smapiot/piral-cloud-node" {
23
6
  */
24
7
  export function createServiceClient(options: CreateServiceClientOptions): FeedServiceApiClient;
25
8
 
9
+ /**
10
+ * The options for creating a new service client.
11
+ */
12
+ export type CreateServiceClientOptions = CreateServiceClientBaseOptions & CreateServiceClientApiKeyOptions;
13
+
26
14
  export class FeedServiceApiClient {
27
15
  constructor(private http: FetchClient, host?: string);
28
16
  getUrl(path: string): string;
@@ -105,6 +93,23 @@ declare module "@smapiot/piral-cloud-node" {
105
93
  doDownloadFile(fileUrl: string, ac?: AbortController): Promise<Blob>;
106
94
  }
107
95
 
96
+ /**
97
+ * The basic / core options for creating a new service client.
98
+ */
99
+ export interface CreateServiceClientBaseOptions {
100
+ /**
101
+ * Defines the host to talk to. Set this if you want to communicate
102
+ * with your own feed service instance.
103
+ * @default https://feed.piral.cloud
104
+ */
105
+ host?: string;
106
+ }
107
+
108
+ /**
109
+ * The API key options for creating a new service client.
110
+ */
111
+ export type CreateServiceClientApiKeyOptions = CreateServiceClientSimpleApiKeyOptions | CreateServiceClientAdvancedApiKeyOptions;
112
+
108
113
  export interface FetchClient {
109
114
  getAuthorizationHeader(): Promise<string>;
110
115
  fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
@@ -532,6 +537,32 @@ declare module "@smapiot/piral-cloud-node" {
532
537
  }>;
533
538
  }
534
539
 
540
+ /**
541
+ * The simple API key options for creating a new service client.
542
+ */
543
+ export interface CreateServiceClientSimpleApiKeyOptions {
544
+ /**
545
+ * Defines the API key to use for making the HTTP calls. Needs
546
+ * to be provided.
547
+ */
548
+ apiKey: string;
549
+ }
550
+
551
+ /**
552
+ * The advanced API key options for creating a new service client.
553
+ */
554
+ export interface CreateServiceClientAdvancedApiKeyOptions {
555
+ /**
556
+ * Defines the API key to use for making the HTTP calls. Needs
557
+ * to be provided.
558
+ */
559
+ resolveApiKey(): Promise<string>;
560
+ /**
561
+ * Defines the type of the resolved API key.
562
+ */
563
+ apiKeyType?: "basic" | "bearer" | "none";
564
+ }
565
+
535
566
  export interface ApiData<T> {
536
567
  items: Array<T>;
537
568
  }
package/lib/index.js CHANGED
@@ -5974,6 +5974,39 @@ function fixResponseChunkedTransferBadEnding(request, errorCallback) {
5974
5974
  });
5975
5975
  }
5976
5976
 
5977
+ // src/utils.ts
5978
+ function createKeyHandler(apiKeyType) {
5979
+ switch (apiKeyType) {
5980
+ case "basic":
5981
+ return (apiKey) => `Basic ${apiKey}`;
5982
+ case "bearer":
5983
+ return (apiKey) => `Bearer ${apiKey}`;
5984
+ default:
5985
+ return (apiKey) => apiKey;
5986
+ }
5987
+ }
5988
+ function createAuthHead(options) {
5989
+ if ("apiKey" in options) {
5990
+ const { apiKey } = options;
5991
+ if (typeof apiKey !== "string") {
5992
+ throw new Error('The field "apiKey" has to be of type string.');
5993
+ }
5994
+ return () => Promise.resolve(`Basic ${apiKey}`);
5995
+ } else if ("resolveApiKey" in options) {
5996
+ const { resolveApiKey, apiKeyType = "basic" } = options;
5997
+ if (typeof options.resolveApiKey !== "function") {
5998
+ throw new Error('The field "resolveApiKey" has to be of type function.');
5999
+ }
6000
+ if (typeof apiKeyType !== "string") {
6001
+ throw new Error('The field "apiKeyType" has to be of type string.');
6002
+ }
6003
+ const makeKey = createKeyHandler(apiKeyType);
6004
+ return () => resolveApiKey().then(makeKey);
6005
+ } else {
6006
+ throw new Error('You must either specify "apikey" or "resolveApiKey".');
6007
+ }
6008
+ }
6009
+
5977
6010
  // src/index.ts
5978
6011
  function createServiceClient(options) {
5979
6012
  if (typeof options === "undefined") {
@@ -5982,20 +6015,16 @@ function createServiceClient(options) {
5982
6015
  if (typeof options !== "object") {
5983
6016
  throw new Error('The "options" have to be an object containing an "apiKey" field.');
5984
6017
  }
5985
- const { host = "https://feed.piral.cloud", apiKey } = options;
6018
+ const { host = "https://feed.piral.cloud" } = options;
5986
6019
  if (typeof host !== "string") {
5987
6020
  throw new Error('The field "host" has to be of type string.');
5988
6021
  }
5989
- if (typeof apiKey !== "string") {
5990
- throw new Error('The field "apiKey" has to be of type string.');
5991
- }
6022
+ const getAuthorizationHeader = createAuthHead(options);
5992
6023
  const http3 = {
5993
6024
  fetch,
5994
6025
  FormData,
5995
6026
  Headers,
5996
- getAuthorizationHeader() {
5997
- return Promise.resolve(apiKey);
5998
- }
6027
+ getAuthorizationHeader
5999
6028
  };
6000
6029
  return new FeedServiceApiClient(http3, host);
6001
6030
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smapiot/piral-cloud-node",
3
- "version": "0.12.2",
3
+ "version": "0.12.3-pre.20220810.2",
4
4
  "description": "Piral Cloud: Node-usable API Client for the Piral Feed Service.",
5
5
  "author": {
6
6
  "name": "smapiot",
@@ -40,7 +40,7 @@
40
40
  "node"
41
41
  ],
42
42
  "devDependencies": {
43
- "@piral/feed-client": "^0.12.2",
43
+ "@piral/feed-client": "^0.12.3",
44
44
  "dets": "^0.12.0",
45
45
  "esbuild": "^0.14.51",
46
46
  "node-fetch": "^3.2.10"