@smapiot/piral-cloud-node 0.12.3-pre.20220810.1 → 0.12.3

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/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;
@@ -56,7 +44,7 @@ declare module "@smapiot/piral-cloud-node" {
56
44
  doRevokeGeneralApiKey(id: string): Promise<unknown>;
57
45
  doRevokeApiKey(feed: string, id: string): Promise<unknown>;
58
46
  doRevokeFeedApiKey(feed: string, id: string): Promise<unknown>;
59
- doDeletePilet(feed: string, id: string): Promise<unknown>;
47
+ doDeletePilet(feed: string, id: string, all?: boolean): Promise<unknown>;
60
48
  doDeleteRule(feed: string, id: string): Promise<unknown>;
61
49
  doUpdateRule(feed: string, id: string, content: RuleUpdateDetails): Promise<unknown>;
62
50
  doUpdatePilet(feed: string, id: string, content: PiletUpdateDetails): Promise<unknown>;
@@ -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
@@ -4588,8 +4588,8 @@ var FeedServiceApiClient = class {
4588
4588
  doRevokeFeedApiKey(feed, id) {
4589
4589
  return this.doDelete(`feed/${feed}/api-keys/${id}`);
4590
4590
  }
4591
- doDeletePilet(feed, id) {
4592
- return this.doDelete(`feed/${feed}/pilets/${id}`);
4591
+ doDeletePilet(feed, id, all = false) {
4592
+ return this.doDelete(`feed/${feed}/pilets/${id}?all=${all}`);
4593
4593
  }
4594
4594
  doDeleteRule(feed, id) {
4595
4595
  return this.doDelete(`feed/${feed}/rules/${id}`);
@@ -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(`Basic ${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.3-pre.20220810.1",
3
+ "version": "0.12.3",
4
4
  "description": "Piral Cloud: Node-usable API Client for the Piral Feed Service.",
5
5
  "author": {
6
6
  "name": "smapiot",