@worknice/js-sdk 0.0.6 → 0.0.7
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/dist/{_types-kBsjg67t.d.ts → _types-Cobjq3B-.d.ts} +64 -0
- package/dist/api/WorkniceIntegrationLogger.d.ts +3 -1
- package/dist/api/WorkniceIntegrationLogger.js.map +1 -1
- package/dist/api/_generated.d.ts +261 -40
- package/dist/api/_generated.js +15 -12
- package/dist/api/_generated.js.map +1 -1
- package/dist/api/_types.d.ts +2 -2
- package/dist/api/createApiOperation.d.ts +1 -1
- package/dist/api/createWorkniceClient.d.ts +1 -1
- package/dist/api/createWorkniceClient.js +2 -0
- package/dist/api/createWorkniceClient.js.map +1 -1
- package/dist/api/operations/activateIntegration.d.ts +1 -1
- package/dist/api/operations/authorizeIntegration.d.ts +1 -1
- package/dist/api/operations/completeSync.d.ts +1 -1
- package/dist/api/operations/createApiToken.d.ts +1 -1
- package/dist/api/operations/createDataImport.d.ts +1 -1
- package/dist/api/operations/createPersonConnection.d.ts +1 -1
- package/dist/api/operations/createPersonDataImportLine.d.ts +1 -1
- package/dist/api/operations/deleteApiToken.d.ts +1 -1
- package/dist/api/operations/deletePersonConnection.d.ts +1 -1
- package/dist/api/operations/getApiTokens.d.ts +1 -1
- package/dist/api/operations/getIntegration.d.ts +1 -1
- package/dist/api/operations/getMyApps.d.ts +1 -1
- package/dist/api/operations/getPeople.d.ts +2 -1
- package/dist/api/operations/getPeople.js +1 -0
- package/dist/api/operations/getPeople.js.map +1 -1
- package/dist/api/operations/getPersonConnections.d.ts +1 -1
- package/dist/api/operations/initializeIntegration.d.ts +1 -1
- package/dist/api/operations/updatePerson.d.ts +70 -0
- package/dist/api/operations/updatePerson.js +90 -0
- package/dist/api/operations/updatePerson.js.map +1 -0
- package/dist/api/operations/updatePersonConnection.d.ts +1 -1
- package/dist/api/operations/updatePersonConnection.js.map +1 -1
- package/dist/employee-records/EQUALITY_CHECKS.js +5 -0
- package/dist/employee-records/EQUALITY_CHECKS.js.map +1 -1
- package/dist/employee-records/_types.d.ts +11 -5
- package/dist/employee-records/comparePersonDataTransferLines.js +14 -0
- package/dist/employee-records/comparePersonDataTransferLines.js.map +1 -1
- package/dist/employee-records/index.d.ts +1 -1
- package/dist/employee-records/mergePersonDataTransferLines.js +1 -0
- package/dist/employee-records/mergePersonDataTransferLines.js.map +1 -1
- package/dist/employee-records/validatePersonDataTransferLine.js +2 -0
- package/dist/employee-records/validatePersonDataTransferLine.js.map +1 -1
- package/dist/employee-records/validatePersonDataTransferLineProfile.d.ts +6 -0
- package/dist/employee-records/validatePersonDataTransferLineProfile.js +17 -0
- package/dist/employee-records/validatePersonDataTransferLineProfile.js.map +1 -0
- package/dist/helpers/_types.d.ts +1 -1
- package/dist/helpers/handleApproveLeaveRequestWebhook.d.ts +1 -1
- package/dist/helpers/handleCancelLeaveRequestWebhook.d.ts +1 -1
- package/dist/helpers/handleCreateIntegrationWebhook.d.ts +1 -1
- package/dist/helpers/handleCreateLeaveRequestWebhook.d.ts +1 -1
- package/dist/helpers/handleDenyLeaveRequestWebhook.d.ts +1 -1
- package/dist/helpers/handleGetAuthorizationUrlWebhook.d.ts +1 -1
- package/dist/helpers/handleGetLeaveBalancesWebhook.d.ts +1 -1
- package/dist/helpers/handleGetLeaveCategoriesWebhook.d.ts +1 -1
- package/dist/helpers/handleGetLeaveRequestHoursWebhook.d.ts +1 -1
- package/dist/helpers/handleProcessLeaveRequestWebhook.d.ts +1 -1
- package/dist/helpers/handleRequest.d.ts +1 -1
- package/dist/helpers/handleRequestWithWorknice.d.ts +1 -1
- package/dist/helpers/handleTriggerIntegrationSyncWebhook.d.ts +7 -8
- package/dist/helpers/handleTriggerIntegrationSyncWebhook.js +180 -117
- package/dist/helpers/handleTriggerIntegrationSyncWebhook.js.map +1 -1
- package/dist/helpers/index.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/utils/errorToString.js +2 -4
- package/dist/utils/errorToString.js.map +1 -1
- package/package.json +3 -3
|
@@ -383,6 +383,7 @@ declare const createWorkniceClient: (clientConfig?: ClientConfig) => {
|
|
|
383
383
|
personalEmail?: string | null;
|
|
384
384
|
personalPhone?: string | null;
|
|
385
385
|
profileEmail?: string | null;
|
|
386
|
+
profilePhone?: string | null;
|
|
386
387
|
residencyStatus?: TaxResidencyStatus | null;
|
|
387
388
|
residentialAddressCity?: string | null;
|
|
388
389
|
residentialAddressCountry?: string | null;
|
|
@@ -507,6 +508,69 @@ declare const createWorkniceClient: (clientConfig?: ClientConfig) => {
|
|
|
507
508
|
};
|
|
508
509
|
person: null;
|
|
509
510
|
}))[]>;
|
|
511
|
+
updatePerson: (variables: Exact<{
|
|
512
|
+
displayName?: InputMaybe<Scalars["String"]["input"]>;
|
|
513
|
+
employeeCode?: InputMaybe<Scalars["String"]["input"]>;
|
|
514
|
+
personId: Scalars["ID"]["input"];
|
|
515
|
+
profileEmail?: InputMaybe<Scalars["String"]["input"]>;
|
|
516
|
+
profilePhone?: InputMaybe<Scalars["String"]["input"]>;
|
|
517
|
+
}>, fetchOptions?: FetchOptions) => Promise<{
|
|
518
|
+
bankAccount1Allocation?: string | null;
|
|
519
|
+
bankAccount1AllocationMethod?: AllocationMethod | null;
|
|
520
|
+
bankAccount1Bsb?: string | null;
|
|
521
|
+
bankAccount1Name?: string | null;
|
|
522
|
+
bankAccount1Number?: string | null;
|
|
523
|
+
bankAccount2Allocation?: string | null;
|
|
524
|
+
bankAccount2AllocationMethod?: AllocationMethod | null;
|
|
525
|
+
bankAccount2Bsb?: string | null;
|
|
526
|
+
bankAccount2Name?: string | null;
|
|
527
|
+
bankAccount2Number?: string | null;
|
|
528
|
+
bankAccount3Allocation?: string | null;
|
|
529
|
+
bankAccount3AllocationMethod?: AllocationMethod | null;
|
|
530
|
+
bankAccount3Bsb?: string | null;
|
|
531
|
+
bankAccount3Name?: string | null;
|
|
532
|
+
bankAccount3Number?: string | null;
|
|
533
|
+
bankAccount4Allocation?: string | null;
|
|
534
|
+
bankAccount4AllocationMethod?: AllocationMethod | null;
|
|
535
|
+
bankAccount4Bsb?: string | null;
|
|
536
|
+
bankAccount4Name?: string | null;
|
|
537
|
+
bankAccount4Number?: string | null;
|
|
538
|
+
bankAccount5Allocation?: string | null;
|
|
539
|
+
bankAccount5AllocationMethod?: AllocationMethod | null;
|
|
540
|
+
bankAccount5Bsb?: string | null;
|
|
541
|
+
bankAccount5Name?: string | null;
|
|
542
|
+
bankAccount5Number?: string | null;
|
|
543
|
+
dateOfBirth?: string | null;
|
|
544
|
+
displayName: string;
|
|
545
|
+
employeeCode?: string | null;
|
|
546
|
+
endDate?: string | null;
|
|
547
|
+
familyName?: string | null;
|
|
548
|
+
gender?: Gender | null;
|
|
549
|
+
givenName?: string | null;
|
|
550
|
+
hasLoanOrStudentDebt?: boolean | null;
|
|
551
|
+
id: string;
|
|
552
|
+
otherGivenNames?: string | null;
|
|
553
|
+
personalEmail?: string | null;
|
|
554
|
+
personalPhone?: string | null;
|
|
555
|
+
profileEmail?: string | null;
|
|
556
|
+
profilePhone?: string | null;
|
|
557
|
+
residencyStatus?: TaxResidencyStatus | null;
|
|
558
|
+
residentialAddressCity?: string | null;
|
|
559
|
+
residentialAddressCountry?: string | null;
|
|
560
|
+
residentialAddressLine1?: string | null;
|
|
561
|
+
residentialAddressLine2?: string | null;
|
|
562
|
+
residentialAddressPostcode?: string | null;
|
|
563
|
+
residentialAddressState?: string | null;
|
|
564
|
+
startDate?: string | null;
|
|
565
|
+
superFund1MemberName?: string | null;
|
|
566
|
+
superFund1MemberNumber?: string | null;
|
|
567
|
+
superFund1Nomination?: SuperFundNomination | null;
|
|
568
|
+
superFund1Usi?: string | null;
|
|
569
|
+
taxFileNumber?: string | null;
|
|
570
|
+
taxFileNumberExemption?: TaxFileNumberExemption | null;
|
|
571
|
+
taxFreeThresholdClaimed?: boolean | null;
|
|
572
|
+
updatedAt: string;
|
|
573
|
+
}>;
|
|
510
574
|
updatePersonConnection: (variables: {
|
|
511
575
|
personConnectionId: string;
|
|
512
576
|
personId: string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { W as WorkniceClient } from '../_types-
|
|
1
|
+
import { W as WorkniceClient } from '../_types-Cobjq3B-.js';
|
|
2
2
|
import './_generated.js';
|
|
3
3
|
|
|
4
4
|
type Logger = Pick<Console, "debug" | "error" | "info" | "warn"> & Partial<Pick<Console, "group" | "groupEnd">>;
|
|
@@ -10,6 +10,8 @@ type WorkniceIntegrationLoggerOptions = {
|
|
|
10
10
|
*
|
|
11
11
|
* Irrespective of this option, debug messages are only logged to the
|
|
12
12
|
* configured logger and are never sent to the Worknice integration logs.
|
|
13
|
+
*
|
|
14
|
+
* Defaults to "debug".
|
|
13
15
|
*/
|
|
14
16
|
debugLogType: "debug" | "info";
|
|
15
17
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/api/WorkniceIntegrationLogger.ts"],"sourcesContent":["import gql from \"dedent\";\nimport { Temporal } from \"temporal-polyfill\";\nimport { v4 as uuid } from \"uuid\";\nimport errorToString from \"../utils/errorToString.js\";\nimport type { WorkniceClient } from \"./_types.js\";\n\nexport type Logger = Pick<Console, \"debug\" | \"error\" | \"info\" | \"warn\"> &\n Partial<Pick<Console, \"group\" | \"groupEnd\">>;\n\nexport type WorkniceIntegrationLoggerOptions = {\n /**\n * The type of log to use for debug messages. If set to \"debug\", debug\n * messages will be logged using the logger.debug() method, but if set to\n * \"info\", debug messages will be logged using the logger.info() method.\n *\n * Irrespective of this option, debug messages are only logged to the\n * configured logger and are never sent to the Worknice integration logs.\n */\n debugLogType: \"debug\" | \"info\";\n\n /**\n * The delay between flushes in milliseconds. Defaults to 1000 (1 second).\n */\n delayBetweenFlushes: number;\n\n /**\n * A Console-like object with which messages will be logger.\n */\n logger: Logger;\n};\n\nclass WorkniceIntegrationLogger {\n /**\n * The Worknice client used to send the logs to the server. If null, the logs\n * will be buffered until the logger is initialized.\n */\n private client: WorkniceClient | null = null;\n\n /**\n * A promise used to ensure that only one request is in progress at a time. If\n * a flush is called while a request is in progress, the flush will wait for\n * the request to finish before starting a new one.\n */\n private currentRequest: Promise<unknown> | null = null;\n\n /**\n * The integration ID the logs will be associated with. If undefined, the\n * logs will be buffered until the logger has been initialized.\n */\n private integrationId: string | undefined = undefined;\n\n /**\n * The random ID for the integration log to which the logs will be appended.\n */\n private integrationLogId: string;\n\n /**\n * Determines when the logger should flush the logs. Calling info(), error(),\n * warn(), indent() or dedent() will automatically flush the logs if the last\n * flush was more than 1 second ago.\n */\n private lastFlush: Temporal.Instant | null = null;\n\n /**\n * A flag is used to determine whether a new log should be created when the\n * logger is flushed. The first time the logger is flushed, a new log will be\n * created. Subsequent flushes will append to the existing log.\n */\n private logCreated = false;\n\n /**\n * Backlog of log messages that have not yet been sent to Worknice. The logs\n * will be sent to the server when the logger is flushed and the backlog will\n * be cleared.\n */\n private logs: Array<{ time: Temporal.Instant; message: string }> = [];\n\n /**\n * Determines the current indentation level of the log messages. The indent()\n * and dedent() functions can be used to increase or decrease the indentation\n * level.\n */\n private messageIndent = 0;\n\n private options: WorkniceIntegrationLoggerOptions;\n\n private timeout: NodeJS.Timeout | null = null;\n\n /**\n * A logger can log messages to both a Console-like object and to a Worknice\n * integration's logs which can be view by a Worknice user. By default,\n * messages are only logged to the configured logger, but when a Worknice\n * integration is connected using the connect() method, the messages will also\n * be sent to the Worknice integration logs.\n * @param logger A Console-like object with which messages can be logged.\n * @example\n * ```js\n * const logger = new WorkniceIntegrationLogger({ logger: console });\n * ```\n */\n constructor(options?: Partial<WorkniceIntegrationLoggerOptions>) {\n this.integrationLogId = uuid();\n this.options = {\n debugLogType: options?.debugLogType ?? \"debug\",\n delayBetweenFlushes: options?.delayBetweenFlushes ?? 1000,\n logger: options?.logger ?? console,\n };\n }\n\n /**\n * Connects the logger to a Worknice integration. If there are previously\n * logged messages that need to be flushed, a new integration log will be\n * instantly created and the messages flushed to it. Otherwise, an integration\n * log will be created if/when the first message is logged.\n */\n public connect(client: WorkniceClient, integrationId: string) {\n this.client = client;\n this.integrationId = integrationId;\n this.flush();\n }\n\n /**\n * Logs a debug message. Debug messages are only logged to the configured logger\n * and are never sent to the Worknice integration logs.\n */\n public debug(message: string) {\n if (this.options.debugLogType === \"debug\") this.options.logger.debug(message);\n else this.options.logger.info(message);\n }\n\n public dedent(message?: string) {\n if (message !== undefined) this.info(message);\n if (this.options.logger.groupEnd) this.options.logger.groupEnd();\n this.messageIndent = Math.max(0, this.messageIndent - 1);\n }\n\n private enqueue(message: string) {\n this.logs.push({\n time: Temporal.Now.instant(),\n message: message\n .split(\"\\n\")\n .map((line) => `${\" \".repeat(this.messageIndent * 2)}${line}`)\n .join(\"\\n\"),\n });\n\n // If the last flush was more than the delay between flushes, flush the logs\n // immediately. Otherwise, schedule a new flush unless one is already\n // scheduled.\n if (this.timeSinceLastFlush() >= this.options.delayBetweenFlushes) {\n this.flush();\n } else if (this.timeout === null) {\n this.timeout = setTimeout(() => {\n if (this.timeSinceLastFlush() >= this.options.delayBetweenFlushes) {\n this.flush();\n }\n }, this.options.delayBetweenFlushes - this.timeSinceLastFlush());\n }\n }\n\n /**\n * Logs an error of any type. The error will be logged to the configured logger\n * with full stack traces and a chain of error causes. A simplified error\n * message will sent to the Worknice integration logs (if connected) and\n * returned for displaying to the user. If the error is not an instance of\n * Error, it will be coerced into a string before being logged.\n */\n public error(error: unknown) {\n const verboseMessage = errorToString(error, {\n format: \"chain\",\n stack: true,\n });\n\n const simpleMessage = errorToString(error, {\n format: \"simple\",\n stack: false,\n });\n\n this.options.logger.error(verboseMessage);\n this.enqueue(simpleMessage);\n\n return simpleMessage;\n }\n\n /**\n * Flushes the log buffer to the server. The info() function will\n * automatically flush the logs if the last flush was more than 1 second\n * ago, but the flush() function should be called manually at the end of the\n * script to ensure that all logs are sent to the server.\n */\n public async flush() {\n // Clear any scheduled flushes.\n if (this.timeout) {\n clearTimeout(this.timeout);\n this.timeout = null;\n }\n\n // Update the last flush time to the current time so that any calls to\n // enqueue will not unnecessarily try to flush the logs again.\n this.lastFlush = Temporal.Now.instant();\n\n // If there's already a request in progress, wait for it to finish before\n // starting a new one.\n if (this.currentRequest) await this.currentRequest;\n\n // If there are no pending logs or the logger hasn't been initialized,\n // don't do anything (any logs will remain in the buffer until the next\n // flush).\n if (this.logs.length === 0 || this.client === null || this.integrationId === null) return;\n\n const entries = this.logs.splice(0).map(({ time, message }) => ({\n time: time.toString({ timeZone: \"Australia/Sydney\", smallestUnit: \"second\" }),\n message,\n }));\n\n this.currentRequest = this.client.fetchFromApi(\n gql`\n mutation AppendIntegrationLog(\n $createNewLog: Boolean!\n $entries: [IntegrationLogInput!]!\n $integrationId: ID!\n $integrationLogId: ID!\n ) {\n createIntegrationLog(integrationId: $integrationId, integrationLogId: $integrationLogId)\n @include(if: $createNewLog) {\n id\n }\n appendIntegrationLog(integrationLogId: $integrationLogId, entries: $entries) {\n id\n }\n }\n `,\n {\n // Only create a new log the first time the logger is flushed.\n createNewLog: this.logCreated === false,\n entries,\n integrationId: this.integrationId,\n integrationLogId: this.integrationLogId,\n },\n );\n\n // Only create a new log the first time the logger is flushed.\n this.logCreated = true;\n\n await this.currentRequest;\n\n // Clear the request promise so that the next flush can start a new request.\n this.currentRequest = null;\n }\n\n public indent(message?: string) {\n if (message !== undefined) this.info(message);\n if (this.options.logger.group) this.options.logger.group();\n this.messageIndent += 1;\n }\n\n /**\n * Logs an informational message. The message will be logged to the configured\n * logger and flushed to the Worknice integration logs if/when connected.\n */\n public info(message: string) {\n this.options.logger.info(message);\n this.enqueue(message);\n }\n\n private timeSinceLastFlush() {\n return this.lastFlush === null\n ? Number.MAX_SAFE_INTEGER\n : Temporal.Now.instant().since(this.lastFlush).total(\"milliseconds\");\n }\n\n public warn(message: string) {\n this.options.logger.warn(message);\n this.enqueue(message);\n }\n}\n\nexport default WorkniceIntegrationLogger;\n"],"mappings":"AAAA,OAAO,SAAS;AAChB,SAAS,gBAAgB;AACzB,SAAS,MAAM,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AAI,CAAC,EAAE,MAAM,QAAQ,OAAO;AAAA,MAC9D,MAAM,KAAK,SAAS,EAAE,UAAU,oBAAoB,cAAc,SAAS,CAAC;AAAA,MAC5E;AAAA,IACF,EAAE;AAEF,SAAK,iBAAiB,KAAK,OAAO;AAAA,MAChC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAgBA;AAAA;AAAA,QAEE,cAAc,KAAK,eAAe;AAAA,QAClC;AAAA,QACA,eAAe,KAAK;AAAA,QACpB,kBAAkB,KAAK;AAAA,MACzB;AAAA,IACF;AAGA,SAAK,aAAa;AAElB,UAAM,KAAK;AAGX,SAAK,iBAAiB;AAAA,EACxB;AAAA,EAEO,OAAO,SAAkB;AAC9B,QAAI,YAAY,OAAW,MAAK,KAAK,OAAO;AAC5C,QAAI,KAAK,QAAQ,OAAO,MAAO,MAAK,QAAQ,OAAO,MAAM;AACzD,SAAK,iBAAiB;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,KAAK,SAAiB;AAC3B,SAAK,QAAQ,OAAO,KAAK,OAAO;AAChC,SAAK,QAAQ,OAAO;AAAA,EACtB;AAAA,EAEQ,qBAAqB;AAC3B,WAAO,KAAK,cAAc,OACtB,OAAO,mBACP,SAAS,IAAI,QAAQ,EAAE,MAAM,KAAK,SAAS,EAAE,MAAM,cAAc;AAAA,EACvE;AAAA,EAEO,KAAK,SAAiB;AAC3B,SAAK,QAAQ,OAAO,KAAK,OAAO;AAChC,SAAK,QAAQ,OAAO;AAAA,EACtB;AACF;AAEA,IAAO,oCAAQ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/api/WorkniceIntegrationLogger.ts"],"sourcesContent":["import gql from \"dedent\";\nimport { Temporal } from \"temporal-polyfill\";\nimport { v4 as uuid } from \"uuid\";\nimport errorToString from \"../utils/errorToString.js\";\nimport type { WorkniceClient } from \"./_types.js\";\n\nexport type Logger = Pick<Console, \"debug\" | \"error\" | \"info\" | \"warn\"> &\n Partial<Pick<Console, \"group\" | \"groupEnd\">>;\n\nexport type WorkniceIntegrationLoggerOptions = {\n /**\n * The type of log to use for debug messages. If set to \"debug\", debug\n * messages will be logged using the logger.debug() method, but if set to\n * \"info\", debug messages will be logged using the logger.info() method.\n *\n * Irrespective of this option, debug messages are only logged to the\n * configured logger and are never sent to the Worknice integration logs.\n *\n * Defaults to \"debug\".\n */\n debugLogType: \"debug\" | \"info\";\n\n /**\n * The delay between flushes in milliseconds. Defaults to 1000 (1 second).\n */\n delayBetweenFlushes: number;\n\n /**\n * A Console-like object with which messages will be logger.\n */\n logger: Logger;\n};\n\nclass WorkniceIntegrationLogger {\n /**\n * The Worknice client used to send the logs to the server. If null, the logs\n * will be buffered until the logger is initialized.\n */\n private client: WorkniceClient | null = null;\n\n /**\n * A promise used to ensure that only one request is in progress at a time. If\n * a flush is called while a request is in progress, the flush will wait for\n * the request to finish before starting a new one.\n */\n private currentRequest: Promise<unknown> | null = null;\n\n /**\n * The integration ID the logs will be associated with. If undefined, the\n * logs will be buffered until the logger has been initialized.\n */\n private integrationId: string | undefined = undefined;\n\n /**\n * The random ID for the integration log to which the logs will be appended.\n */\n private integrationLogId: string;\n\n /**\n * Determines when the logger should flush the logs. Calling info(), error(),\n * warn(), indent() or dedent() will automatically flush the logs if the last\n * flush was more than 1 second ago.\n */\n private lastFlush: Temporal.Instant | null = null;\n\n /**\n * A flag is used to determine whether a new log should be created when the\n * logger is flushed. The first time the logger is flushed, a new log will be\n * created. Subsequent flushes will append to the existing log.\n */\n private logCreated = false;\n\n /**\n * Backlog of log messages that have not yet been sent to Worknice. The logs\n * will be sent to the server when the logger is flushed and the backlog will\n * be cleared.\n */\n private logs: Array<{ time: Temporal.Instant; message: string }> = [];\n\n /**\n * Determines the current indentation level of the log messages. The indent()\n * and dedent() functions can be used to increase or decrease the indentation\n * level.\n */\n private messageIndent = 0;\n\n private options: WorkniceIntegrationLoggerOptions;\n\n private timeout: NodeJS.Timeout | null = null;\n\n /**\n * A logger can log messages to both a Console-like object and to a Worknice\n * integration's logs which can be view by a Worknice user. By default,\n * messages are only logged to the configured logger, but when a Worknice\n * integration is connected using the connect() method, the messages will also\n * be sent to the Worknice integration logs.\n * @param logger A Console-like object with which messages can be logged.\n * @example\n * ```js\n * const logger = new WorkniceIntegrationLogger({ logger: console });\n * ```\n */\n constructor(options?: Partial<WorkniceIntegrationLoggerOptions>) {\n this.integrationLogId = uuid();\n this.options = {\n debugLogType: options?.debugLogType ?? \"debug\",\n delayBetweenFlushes: options?.delayBetweenFlushes ?? 1000,\n logger: options?.logger ?? console,\n };\n }\n\n /**\n * Connects the logger to a Worknice integration. If there are previously\n * logged messages that need to be flushed, a new integration log will be\n * instantly created and the messages flushed to it. Otherwise, an integration\n * log will be created if/when the first message is logged.\n */\n public connect(client: WorkniceClient, integrationId: string) {\n this.client = client;\n this.integrationId = integrationId;\n this.flush();\n }\n\n /**\n * Logs a debug message. Debug messages are only logged to the configured logger\n * and are never sent to the Worknice integration logs.\n */\n public debug(message: string) {\n if (this.options.debugLogType === \"debug\") this.options.logger.debug(message);\n else this.options.logger.info(message);\n }\n\n public dedent(message?: string) {\n if (message !== undefined) this.info(message);\n if (this.options.logger.groupEnd) this.options.logger.groupEnd();\n this.messageIndent = Math.max(0, this.messageIndent - 1);\n }\n\n private enqueue(message: string) {\n this.logs.push({\n time: Temporal.Now.instant(),\n message: message\n .split(\"\\n\")\n .map((line) => `${\" \".repeat(this.messageIndent * 2)}${line}`)\n .join(\"\\n\"),\n });\n\n // If the last flush was more than the delay between flushes, flush the logs\n // immediately. Otherwise, schedule a new flush unless one is already\n // scheduled.\n if (this.timeSinceLastFlush() >= this.options.delayBetweenFlushes) {\n this.flush();\n } else if (this.timeout === null) {\n this.timeout = setTimeout(() => {\n if (this.timeSinceLastFlush() >= this.options.delayBetweenFlushes) {\n this.flush();\n }\n }, this.options.delayBetweenFlushes - this.timeSinceLastFlush());\n }\n }\n\n /**\n * Logs an error of any type. The error will be logged to the configured logger\n * with full stack traces and a chain of error causes. A simplified error\n * message will sent to the Worknice integration logs (if connected) and\n * returned for displaying to the user. If the error is not an instance of\n * Error, it will be coerced into a string before being logged.\n */\n public error(error: unknown) {\n const verboseMessage = errorToString(error, {\n format: \"chain\",\n stack: true,\n });\n\n const simpleMessage = errorToString(error, {\n format: \"simple\",\n stack: false,\n });\n\n this.options.logger.error(verboseMessage);\n this.enqueue(simpleMessage);\n\n return simpleMessage;\n }\n\n /**\n * Flushes the log buffer to the server. The info() function will\n * automatically flush the logs if the last flush was more than 1 second\n * ago, but the flush() function should be called manually at the end of the\n * script to ensure that all logs are sent to the server.\n */\n public async flush() {\n // Clear any scheduled flushes.\n if (this.timeout) {\n clearTimeout(this.timeout);\n this.timeout = null;\n }\n\n // Update the last flush time to the current time so that any calls to\n // enqueue will not unnecessarily try to flush the logs again.\n this.lastFlush = Temporal.Now.instant();\n\n // If there's already a request in progress, wait for it to finish before\n // starting a new one.\n if (this.currentRequest) await this.currentRequest;\n\n // If there are no pending logs or the logger hasn't been initialized,\n // don't do anything (any logs will remain in the buffer until the next\n // flush).\n if (this.logs.length === 0 || this.client === null || this.integrationId === null) return;\n\n const entries = this.logs.splice(0).map(({ time, message }) => ({\n time: time.toString({ timeZone: \"Australia/Sydney\", smallestUnit: \"second\" }),\n message,\n }));\n\n this.currentRequest = this.client.fetchFromApi(\n gql`\n mutation AppendIntegrationLog(\n $createNewLog: Boolean!\n $entries: [IntegrationLogInput!]!\n $integrationId: ID!\n $integrationLogId: ID!\n ) {\n createIntegrationLog(integrationId: $integrationId, integrationLogId: $integrationLogId)\n @include(if: $createNewLog) {\n id\n }\n appendIntegrationLog(integrationLogId: $integrationLogId, entries: $entries) {\n id\n }\n }\n `,\n {\n // Only create a new log the first time the logger is flushed.\n createNewLog: this.logCreated === false,\n entries,\n integrationId: this.integrationId,\n integrationLogId: this.integrationLogId,\n },\n );\n\n // Only create a new log the first time the logger is flushed.\n this.logCreated = true;\n\n await this.currentRequest;\n\n // Clear the request promise so that the next flush can start a new request.\n this.currentRequest = null;\n }\n\n public indent(message?: string) {\n if (message !== undefined) this.info(message);\n if (this.options.logger.group) this.options.logger.group();\n this.messageIndent += 1;\n }\n\n /**\n * Logs an informational message. The message will be logged to the configured\n * logger and flushed to the Worknice integration logs if/when connected.\n */\n public info(message: string) {\n this.options.logger.info(message);\n this.enqueue(message);\n }\n\n private timeSinceLastFlush() {\n return this.lastFlush === null\n ? Number.MAX_SAFE_INTEGER\n : Temporal.Now.instant().since(this.lastFlush).total(\"milliseconds\");\n }\n\n public warn(message: string) {\n this.options.logger.warn(message);\n this.enqueue(message);\n }\n}\n\nexport default WorkniceIntegrationLogger;\n"],"mappings":"AAAA,OAAO,SAAS;AAChB,SAAS,gBAAgB;AACzB,SAAS,MAAM,YAAY;AAC3B,OAAO,mBAAmB;AA8B1B,MAAM,0BAA0B;AAAA;AAAA;AAAA;AAAA;AAAA,EAKtB,SAAgC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOhC,iBAA0C;AAAA;AAAA;AAAA;AAAA;AAAA,EAM1C,gBAAoC;AAAA;AAAA;AAAA;AAAA,EAKpC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,YAAqC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOrC,aAAa;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOb,OAA2D,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAO5D,gBAAgB;AAAA,EAEhB;AAAA,EAEA,UAAiC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAczC,YAAY,SAAqD;AAC/D,SAAK,mBAAmB,KAAK;AAC7B,SAAK,UAAU;AAAA,MACb,cAAc,SAAS,gBAAgB;AAAA,MACvC,qBAAqB,SAAS,uBAAuB;AAAA,MACrD,QAAQ,SAAS,UAAU;AA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