@ah-oh/ao-workspaces-api-sdk 1.0.21 → 1.0.23
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/CHANGELOG.md +17 -0
- package/README.md +88 -3
- package/package.json +2 -2
- package/src/index.d.ts +1 -0
- package/src/index.js +1 -0
- package/src/index.js.map +1 -1
- package/src/lib/auth.module.js +5 -3
- package/src/lib/auth.module.js.map +1 -1
- package/src/lib/print.client.d.ts +75 -0
- package/src/lib/print.client.js +140 -0
- package/src/lib/print.client.js.map +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
+
## 1.0.23 (2026-09-24)
|
|
2
|
+
|
|
3
|
+
### 🚀 Features
|
|
4
|
+
|
|
5
|
+
- **api-sdk:** add PrintClient for hardware connector print jobs ([7b1d334](https://github.com/ah-oh/ao-workspaces/commit/7b1d334))
|
|
6
|
+
- update TypeScript configuration and ESLint migration documentation ([2993a37](https://github.com/ah-oh/ao-workspaces/commit/2993a37))
|
|
7
|
+
|
|
8
|
+
### ❤️ Thank You
|
|
9
|
+
|
|
10
|
+
- Claude Opus 5.5
|
|
11
|
+
- Jan Kessler
|
|
12
|
+
- Jan Keßler @jannks
|
|
13
|
+
|
|
14
|
+
## 1.0.22 (2026-05-21)
|
|
15
|
+
|
|
16
|
+
This was a version bump only for api-sdk to align it with other projects, there were no code changes.
|
|
17
|
+
|
|
1
18
|
## 1.0.21 (2026-03-19)
|
|
2
19
|
|
|
3
20
|
This was a version bump only for api-sdk to align it with other projects, there were no code changes.
|
package/README.md
CHANGED
|
@@ -1,11 +1,96 @@
|
|
|
1
|
-
#
|
|
1
|
+
# api-sdk
|
|
2
2
|
|
|
3
3
|
This library was generated with [Nx](https://nx.dev).
|
|
4
4
|
|
|
5
5
|
## Building
|
|
6
6
|
|
|
7
|
-
Run `nx build
|
|
7
|
+
Run `nx build api-sdk` to build the library.
|
|
8
8
|
|
|
9
9
|
## Running unit tests
|
|
10
10
|
|
|
11
|
-
Run `nx test
|
|
11
|
+
Run `nx test api-sdk` to execute the unit tests via [Jest](https://jestjs.io).
|
|
12
|
+
|
|
13
|
+
## Drucken über den Hardware Connector
|
|
14
|
+
|
|
15
|
+
Produkt-Apps drucken über den `PrintClient` auf **Druckziele** der Company
|
|
16
|
+
(z. B. `shipping-label`). Welcher Drucker dahinter steht, legt die Company im
|
|
17
|
+
Admin fest. Fachliche Regeln: `docs/features/hardware-connector/` im Repository
|
|
18
|
+
`ah-oh/ao-workspaces`.
|
|
19
|
+
|
|
20
|
+
Der `PrintClient` wird vom `AuthModule` bereitgestellt (`forRoot` oder
|
|
21
|
+
`forRootAsync`) und meldet sich pro Aufruf mit dem User-Token der App an
|
|
22
|
+
(`tokenId`, `tokenKey`). `product` ist die aufrufende App, **nicht** `admin` –
|
|
23
|
+
nur so gilt der Aufruf als App und der Job trägt die App als Auslöser.
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { PrintClient, PrintClientError } from '@ah-oh/ao-workspaces-api-sdk';
|
|
27
|
+
import { PrintContentType, Product } from '@ah-oh/ao-workspaces-types';
|
|
28
|
+
|
|
29
|
+
const job = await printClient.print({
|
|
30
|
+
tokenId,
|
|
31
|
+
tokenKey,
|
|
32
|
+
product: Product.SZALES,
|
|
33
|
+
companyId,
|
|
34
|
+
target: 'shipping-label',
|
|
35
|
+
contentType: PrintContentType.ZPL,
|
|
36
|
+
content: Buffer.from(zpl),
|
|
37
|
+
fileName: `label-${orderId}.zpl`,
|
|
38
|
+
idempotencyKey: `label-${orderId}`,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const current = await printClient.getJob({
|
|
42
|
+
tokenId,
|
|
43
|
+
tokenKey,
|
|
44
|
+
product: Product.SZALES,
|
|
45
|
+
companyId,
|
|
46
|
+
jobId: job._id,
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
| Methode | Zweck |
|
|
51
|
+
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
52
|
+
| `print` | Legt einen Job an und liefert ihn im Zustand `queued`. Optionen: `copies` (1–99), bei PDF `paperSize` und `color`, `idempotencyKey`, `expiresInSeconds`. |
|
|
53
|
+
| `getJob` | Liefert den aktuellen Zustand eines Jobs inklusive `error`. |
|
|
54
|
+
| `waitForJob` | Fragt den Job ab, bis er abgeschlossen ist (`completed`, `failed`, `expired`, `canceled`) oder `timeoutMs` (Standard 60 s) vergangen ist. Meldet sich einmal an. |
|
|
55
|
+
| `isFinal` (statisch) | Prüft, ob ein Job sich nicht mehr ändert. `waitForJob` liefert nach Ablauf der Wartezeit den letzten, noch offenen Zustand. |
|
|
56
|
+
|
|
57
|
+
Hinweise:
|
|
58
|
+
|
|
59
|
+
- **Idempotenz:** Derselbe `idempotencyKey` liefert innerhalb einer Company
|
|
60
|
+
denselben Job zurück, statt erneut zu drucken. Für Wiederholungen nach
|
|
61
|
+
Netzwerkfehlern immer einen Schlüssel mitgeben.
|
|
62
|
+
- **Ohne Verbindung:** Ist der Connector offline, bleibt der Job `queued` und
|
|
63
|
+
wird beim Wiederverbinden gedruckt – bis zur Ablaufzeit (Standard 1 Stunde,
|
|
64
|
+
über `expiresInSeconds` änderbar), danach `expired`.
|
|
65
|
+
- **`completed`** heißt „vom Drucksystem angenommen“, nicht „physisch gedruckt“.
|
|
66
|
+
|
|
67
|
+
### Fehler
|
|
68
|
+
|
|
69
|
+
Fehler der Hardware-API werden als `PrintClientError` geworfen (abgeleitet von
|
|
70
|
+
Nests `HttpException`). `code` enthält den maschinenlesbaren Fehlercode,
|
|
71
|
+
`getStatus()` den HTTP-Status:
|
|
72
|
+
|
|
73
|
+
| `code` | Status | Bedeutung |
|
|
74
|
+
| ----------------------- | ------ | --------------------------------------------------------- |
|
|
75
|
+
| `target_not_found` | 404 | Druckziel gibt es in der Company nicht |
|
|
76
|
+
| `target_unassigned` | 422 | Druckziel ist keinem Drucker zugeordnet |
|
|
77
|
+
| `option_not_allowed` | 422 | `paperSize` oder `color` bei ZPL |
|
|
78
|
+
| `content_type_mismatch` | 422 | Drucker bzw. Druckziel akzeptiert diesen Inhaltstyp nicht |
|
|
79
|
+
| `printer_disabled` | 422 | Zugeordneter Drucker ist nicht freigegeben |
|
|
80
|
+
| `content_missing` | 400 | Kein Inhalt übergeben |
|
|
81
|
+
| `content_invalid` | 422 | Inhalt ist kein gültiges PDF |
|
|
82
|
+
| `content_too_large` | 413 | Inhalt größer als 50 MB |
|
|
83
|
+
| `job_not_found` | 404 | Job-ID unbekannt |
|
|
84
|
+
|
|
85
|
+
Validierungsfehler (400) und Netzwerkfehler (502) haben keinen `code`.
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
try {
|
|
89
|
+
await printClient.print({ ... });
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (error instanceof PrintClientError && error.code === 'target_unassigned') {
|
|
92
|
+
// Hinweis an die Company: Druckziel im Admin einem Drucker zuordnen
|
|
93
|
+
}
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ah-oh/ao-workspaces-api-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.23",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
6
|
"types": "./src/index.d.ts",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"tslib": "^2.3.0",
|
|
17
|
-
"@ah-oh/ao-workspaces-types": "^0.0.
|
|
17
|
+
"@ah-oh/ao-workspaces-types": "^0.0.10"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
20
|
"@nestjs/common": ">=9.0.0",
|
package/src/index.d.ts
CHANGED
package/src/index.js
CHANGED
|
@@ -16,4 +16,5 @@ tslib_1.__exportStar(require("./lib/models/api-key.model"), exports);
|
|
|
16
16
|
tslib_1.__exportStar(require("./lib/auth.module"), exports);
|
|
17
17
|
tslib_1.__exportStar(require("./lib/customer-center.service"), exports);
|
|
18
18
|
tslib_1.__exportStar(require("./lib/mail-settings.service"), exports);
|
|
19
|
+
tslib_1.__exportStar(require("./lib/print.client"), exports);
|
|
19
20
|
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../libs/api-sdk/src/index.ts"],"names":[],"mappings":";;;AAAA,kEAAwC;AACxC,+EAAqD;AACrD,6EAAmD;AACnD,qEAA2C;AAC3C,gFAAsD;AACtD,yEAA+C;AAE/C,+EAAqD;AACrD,oEAA0C;AAC1C,+EAAqD;AACrD,0EAAgD;AAChD,oEAA0C;AAC1C,qEAA2C;AAE3C,4DAAkC;AAClC,wEAA8C;AAC9C,sEAA4C"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../libs/api-sdk/src/index.ts"],"names":[],"mappings":";;;AAAA,kEAAwC;AACxC,+EAAqD;AACrD,6EAAmD;AACnD,qEAA2C;AAC3C,gFAAsD;AACtD,yEAA+C;AAE/C,+EAAqD;AACrD,oEAA0C;AAC1C,+EAAqD;AACrD,0EAAgD;AAChD,oEAA0C;AAC1C,qEAA2C;AAE3C,4DAAkC;AAClC,wEAA8C;AAC9C,sEAA4C;AAC5C,6DAAmC"}
|
package/src/lib/auth.module.js
CHANGED
|
@@ -7,6 +7,7 @@ const common_1 = require("@nestjs/common");
|
|
|
7
7
|
const auth_constants_1 = require("./auth.constants");
|
|
8
8
|
const customer_center_service_1 = require("./customer-center.service");
|
|
9
9
|
const mail_settings_service_1 = require("./mail-settings.service");
|
|
10
|
+
const print_client_1 = require("./print.client");
|
|
10
11
|
var auth_constants_2 = require("./auth.constants");
|
|
11
12
|
Object.defineProperty(exports, "AUTH_MODULE_OPTIONS", { enumerable: true, get: function () { return auth_constants_2.AUTH_MODULE_OPTIONS; } });
|
|
12
13
|
Object.defineProperty(exports, "AUTH_TOKEN", { enumerable: true, get: function () { return auth_constants_2.AUTH_TOKEN; } });
|
|
@@ -28,8 +29,9 @@ let AuthModule = AuthModule_1 = class AuthModule {
|
|
|
28
29
|
provider,
|
|
29
30
|
customer_center_service_1.CustomerCenterService,
|
|
30
31
|
mail_settings_service_1.MailSettingsService,
|
|
32
|
+
print_client_1.PrintClient,
|
|
31
33
|
],
|
|
32
|
-
exports: [provider, customer_center_service_1.CustomerCenterService, mail_settings_service_1.MailSettingsService],
|
|
34
|
+
exports: [provider, customer_center_service_1.CustomerCenterService, mail_settings_service_1.MailSettingsService, print_client_1.PrintClient],
|
|
33
35
|
};
|
|
34
36
|
}
|
|
35
37
|
static forRootAsync(options) {
|
|
@@ -41,8 +43,8 @@ let AuthModule = AuthModule_1 = class AuthModule {
|
|
|
41
43
|
return {
|
|
42
44
|
module: AuthModule_1,
|
|
43
45
|
imports: options.imports,
|
|
44
|
-
providers: [...this.createAsyncProviders(options), provider, customer_center_service_1.CustomerCenterService, mail_settings_service_1.MailSettingsService],
|
|
45
|
-
exports: [provider, customer_center_service_1.CustomerCenterService, mail_settings_service_1.MailSettingsService],
|
|
46
|
+
providers: [...this.createAsyncProviders(options), provider, customer_center_service_1.CustomerCenterService, mail_settings_service_1.MailSettingsService, print_client_1.PrintClient],
|
|
47
|
+
exports: [provider, customer_center_service_1.CustomerCenterService, mail_settings_service_1.MailSettingsService, print_client_1.PrintClient],
|
|
46
48
|
};
|
|
47
49
|
}
|
|
48
50
|
static createAsyncProviders(options) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.module.js","sourceRoot":"","sources":["../../../../../libs/api-sdk/src/lib/auth.module.ts"],"names":[],"mappings":";;;;;AAAA,2CAA+E;AAE/E,qDAAmE;AACnE,uEAAkE;AAClE,mEAA8D;
|
|
1
|
+
{"version":3,"file":"auth.module.js","sourceRoot":"","sources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PrintContentType, PrintPaperSize, Product } from '@ah-oh/ao-workspaces-types';
|
|
2
|
+
import { HttpException } from '@nestjs/common';
|
|
3
|
+
import { CustomerCenterService } from './customer-center.service';
|
|
4
|
+
import { TokenCredentials } from './mail-settings.service';
|
|
5
|
+
import { AuthModuleOptions } from './models/auth.options';
|
|
6
|
+
/**
|
|
7
|
+
* Credentials of a product app. `product` must be the calling app (e.g. `szales`),
|
|
8
|
+
* not `admin`: only app sessions may print on print targets without the admin
|
|
9
|
+
* permission `hardware`, and the job records the app as its origin.
|
|
10
|
+
*/
|
|
11
|
+
export interface PrintCredentials extends TokenCredentials {
|
|
12
|
+
product: Product;
|
|
13
|
+
}
|
|
14
|
+
export interface PrintArgs extends PrintCredentials {
|
|
15
|
+
companyId: string;
|
|
16
|
+
/** Key of a print target configured by the company, e.g. `shipping-label`. */
|
|
17
|
+
target: string;
|
|
18
|
+
contentType: PrintContentType;
|
|
19
|
+
/** Print-ready content: a PDF document or raw ZPL. At most 50 MB. */
|
|
20
|
+
content: Buffer | Uint8Array;
|
|
21
|
+
/** File name shown in the print history. Defaults to `content.<contentType>`. */
|
|
22
|
+
fileName?: string;
|
|
23
|
+
/** 1–99, default 1. */
|
|
24
|
+
copies?: number;
|
|
25
|
+
/** PDF only. */
|
|
26
|
+
paperSize?: PrintPaperSize;
|
|
27
|
+
/** PDF only. */
|
|
28
|
+
color?: PrintColorMode;
|
|
29
|
+
/** The same key within a company always returns the same job instead of printing again. */
|
|
30
|
+
idempotencyKey?: string;
|
|
31
|
+
/** 10–604800 seconds; the job expires unprinted afterwards. Default on the server: 3600. */
|
|
32
|
+
expiresInSeconds?: number;
|
|
33
|
+
}
|
|
34
|
+
export interface GetPrintJobArgs extends PrintCredentials {
|
|
35
|
+
companyId: string;
|
|
36
|
+
jobId: string;
|
|
37
|
+
}
|
|
38
|
+
export interface WaitForPrintJobArgs extends GetPrintJobArgs {
|
|
39
|
+
/** Maximum time to wait. Default 60000 ms. */
|
|
40
|
+
timeoutMs?: number;
|
|
41
|
+
/** Pause between two polls. Default 2000 ms. */
|
|
42
|
+
intervalMs?: number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Error of the hardware API. `code` is the machine-readable code of the body
|
|
46
|
+
* `{ type: 'error', code, message }` (e.g. `target_unassigned`,
|
|
47
|
+
* `target_not_found`, `option_not_allowed`, `content_too_large`). It is
|
|
48
|
+
* undefined for errors without such a body (validation, network).
|
|
49
|
+
*/
|
|
50
|
+
export declare class PrintClientError extends HttpException {
|
|
51
|
+
readonly code: string | undefined;
|
|
52
|
+
constructor(code: string | undefined, message: string, status: number);
|
|
53
|
+
}
|
|
54
|
+
/** Creates print jobs on the hardware connector API and reads their state (HC-12). */
|
|
55
|
+
export declare class PrintClient {
|
|
56
|
+
private readonly options;
|
|
57
|
+
private readonly customerCenterService;
|
|
58
|
+
private readonly BASE_URL;
|
|
59
|
+
constructor(options: AuthModuleOptions, customerCenterService: CustomerCenterService);
|
|
60
|
+
/** True once the job will not change anymore (completed, failed, expired, canceled). */
|
|
61
|
+
static isFinal(job: Pick<IPrintJob, 'state'>): boolean;
|
|
62
|
+
/** Creates a print job on a print target. Resolves as soon as the job is queued. */
|
|
63
|
+
print(args: PrintArgs): Promise<IPrintJob>;
|
|
64
|
+
getJob(args: GetPrintJobArgs): Promise<IPrintJob>;
|
|
65
|
+
/**
|
|
66
|
+
* Polls the job until it reaches a final state or `timeoutMs` has passed.
|
|
67
|
+
* On timeout the last known (non-final) job is returned; check it with
|
|
68
|
+
* `PrintClient.isFinal`. A job for an offline connector stays `queued` until
|
|
69
|
+
* it expires.
|
|
70
|
+
*/
|
|
71
|
+
waitForJob(args: WaitForPrintJobArgs): Promise<IPrintJob>;
|
|
72
|
+
private fetchJob;
|
|
73
|
+
private getAuthorizationHeader;
|
|
74
|
+
private request;
|
|
75
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var PrintClient_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.PrintClient = exports.PrintClientError = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const ao_workspaces_types_1 = require("@ah-oh/ao-workspaces-types");
|
|
7
|
+
const common_1 = require("@nestjs/common");
|
|
8
|
+
const axios_1 = require("axios");
|
|
9
|
+
const auth_constants_1 = require("./auth.constants");
|
|
10
|
+
const customer_center_service_1 = require("./customer-center.service");
|
|
11
|
+
/**
|
|
12
|
+
* Error of the hardware API. `code` is the machine-readable code of the body
|
|
13
|
+
* `{ type: 'error', code, message }` (e.g. `target_unassigned`,
|
|
14
|
+
* `target_not_found`, `option_not_allowed`, `content_too_large`). It is
|
|
15
|
+
* undefined for errors without such a body (validation, network).
|
|
16
|
+
*/
|
|
17
|
+
class PrintClientError extends common_1.HttpException {
|
|
18
|
+
code;
|
|
19
|
+
constructor(code, message, status) {
|
|
20
|
+
super({ type: 'error', code, message }, status);
|
|
21
|
+
this.code = code;
|
|
22
|
+
this.name = 'PrintClientError';
|
|
23
|
+
this.message = message;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.PrintClientError = PrintClientError;
|
|
27
|
+
const CONTENT_MIME_TYPES = {
|
|
28
|
+
[ao_workspaces_types_1.PrintContentType.PDF]: 'application/pdf',
|
|
29
|
+
[ao_workspaces_types_1.PrintContentType.ZPL]: 'application/octet-stream',
|
|
30
|
+
};
|
|
31
|
+
const DEFAULT_WAIT_TIMEOUT_MS = 60_000;
|
|
32
|
+
const DEFAULT_WAIT_INTERVAL_MS = 2_000;
|
|
33
|
+
/** Creates print jobs on the hardware connector API and reads their state (HC-12). */
|
|
34
|
+
let PrintClient = PrintClient_1 = class PrintClient {
|
|
35
|
+
options;
|
|
36
|
+
customerCenterService;
|
|
37
|
+
BASE_URL;
|
|
38
|
+
constructor(options, customerCenterService) {
|
|
39
|
+
this.options = options;
|
|
40
|
+
this.customerCenterService = customerCenterService;
|
|
41
|
+
this.BASE_URL = this.options.baseUrl;
|
|
42
|
+
}
|
|
43
|
+
/** True once the job will not change anymore (completed, failed, expired, canceled). */
|
|
44
|
+
static isFinal(job) {
|
|
45
|
+
return ao_workspaces_types_1.FINAL_PRINT_JOB_STATES.includes(job.state);
|
|
46
|
+
}
|
|
47
|
+
/** Creates a print job on a print target. Resolves as soon as the job is queued. */
|
|
48
|
+
async print(args) {
|
|
49
|
+
const authorization = await this.getAuthorizationHeader(args);
|
|
50
|
+
const form = new FormData();
|
|
51
|
+
form.append('target', args.target);
|
|
52
|
+
form.append('contentType', args.contentType);
|
|
53
|
+
if (args.copies !== undefined)
|
|
54
|
+
form.append('copies', String(args.copies));
|
|
55
|
+
if (args.paperSize)
|
|
56
|
+
form.append('paperSize', args.paperSize);
|
|
57
|
+
if (args.color)
|
|
58
|
+
form.append('color', args.color);
|
|
59
|
+
if (args.idempotencyKey)
|
|
60
|
+
form.append('idempotencyKey', args.idempotencyKey);
|
|
61
|
+
if (args.expiresInSeconds !== undefined)
|
|
62
|
+
form.append('expiresInSeconds', String(args.expiresInSeconds));
|
|
63
|
+
form.append('content', new Blob([args.content], {
|
|
64
|
+
type: CONTENT_MIME_TYPES[args.contentType],
|
|
65
|
+
}), args.fileName ?? `content.${args.contentType}`);
|
|
66
|
+
return this.request({
|
|
67
|
+
method: 'POST',
|
|
68
|
+
url: `/auth-api/companies/${args.companyId}/hardware/print-jobs`,
|
|
69
|
+
headers: { accept: 'application/json', authorization },
|
|
70
|
+
data: form,
|
|
71
|
+
maxBodyLength: Infinity,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
async getJob(args) {
|
|
75
|
+
const authorization = await this.getAuthorizationHeader(args);
|
|
76
|
+
return this.fetchJob(args, authorization);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Polls the job until it reaches a final state or `timeoutMs` has passed.
|
|
80
|
+
* On timeout the last known (non-final) job is returned; check it with
|
|
81
|
+
* `PrintClient.isFinal`. A job for an offline connector stays `queued` until
|
|
82
|
+
* it expires.
|
|
83
|
+
*/
|
|
84
|
+
async waitForJob(args) {
|
|
85
|
+
const timeoutMs = args.timeoutMs ?? DEFAULT_WAIT_TIMEOUT_MS;
|
|
86
|
+
const intervalMs = args.intervalMs ?? DEFAULT_WAIT_INTERVAL_MS;
|
|
87
|
+
const deadline = Date.now() + timeoutMs;
|
|
88
|
+
const authorization = await this.getAuthorizationHeader(args);
|
|
89
|
+
let job = await this.fetchJob(args, authorization);
|
|
90
|
+
while (!PrintClient_1.isFinal(job) && Date.now() + intervalMs <= deadline) {
|
|
91
|
+
await new Promise(resolve => setTimeout(resolve, intervalMs));
|
|
92
|
+
job = await this.fetchJob(args, authorization);
|
|
93
|
+
}
|
|
94
|
+
return job;
|
|
95
|
+
}
|
|
96
|
+
fetchJob(args, authorization) {
|
|
97
|
+
return this.request({
|
|
98
|
+
method: 'GET',
|
|
99
|
+
url: `/auth-api/companies/${args.companyId}/hardware/print-jobs/${args.jobId}`,
|
|
100
|
+
headers: { accept: 'application/json', authorization },
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
async getAuthorizationHeader(credentials) {
|
|
104
|
+
const { token } = await this.customerCenterService.tokenSignIn({
|
|
105
|
+
tokenId: credentials.tokenId,
|
|
106
|
+
tokenKey: credentials.tokenKey,
|
|
107
|
+
product: credentials.product,
|
|
108
|
+
});
|
|
109
|
+
return `Bearer ${token}`;
|
|
110
|
+
}
|
|
111
|
+
async request(config) {
|
|
112
|
+
try {
|
|
113
|
+
const res = await axios_1.default.request({ ...config, baseURL: this.BASE_URL });
|
|
114
|
+
return res.data;
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
throw toPrintClientError(error);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
exports.PrintClient = PrintClient;
|
|
122
|
+
exports.PrintClient = PrintClient = PrintClient_1 = tslib_1.__decorate([
|
|
123
|
+
(0, common_1.Injectable)(),
|
|
124
|
+
tslib_1.__param(0, (0, common_1.Inject)(auth_constants_1.AUTH_MODULE_OPTIONS)),
|
|
125
|
+
tslib_1.__metadata("design:paramtypes", [Object, customer_center_service_1.CustomerCenterService])
|
|
126
|
+
], PrintClient);
|
|
127
|
+
function toPrintClientError(error) {
|
|
128
|
+
if (!(error instanceof axios_1.AxiosError))
|
|
129
|
+
return error;
|
|
130
|
+
const status = error.response?.status ?? common_1.HttpStatus.BAD_GATEWAY;
|
|
131
|
+
const body = error.response?.data;
|
|
132
|
+
const code = typeof body?.code === 'string' ? body.code : undefined;
|
|
133
|
+
const message = Array.isArray(body?.message)
|
|
134
|
+
? body.message.join('; ')
|
|
135
|
+
: typeof body?.message === 'string'
|
|
136
|
+
? body.message
|
|
137
|
+
: error.message;
|
|
138
|
+
return new PrintClientError(code, message, status);
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=print.client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"print.client.js","sourceRoot":"","sources":["../../../../../libs/api-sdk/src/lib/print.client.ts"],"names":[],"mappings":";;;;;AAAA,oEAOoC;AACpC,2CAA+E;AAC/E,iCAA8D;AAE9D,qDAAuD;AACvD,uEAAkE;AA8ClE;;;;;GAKG;AACH,MAAa,gBAAiB,SAAQ,sBAAa;IAE/B;IADlB,YACkB,IAAwB,EACxC,OAAe,EACf,MAAc;QAEd,KAAK,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,MAAM,CAAC,CAAC;QAJhC,SAAI,GAAJ,IAAI,CAAoB;QAKxC,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;QAC/B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;CACF;AAVD,4CAUC;AAED,MAAM,kBAAkB,GAAqC;IAC3D,CAAC,sCAAgB,CAAC,GAAG,CAAC,EAAE,iBAAiB;IACzC,CAAC,sCAAgB,CAAC,GAAG,CAAC,EAAE,0BAA0B;CACnD,C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