@xyd-js/sources 0.0.1-xyd.0

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.
@@ -0,0 +1,193 @@
1
+ /**
2
+ *
3
+ * Includes enums for payment methods and transaction statuses, interfaces for billing transactions and receipts,
4
+ * and services to process transactions, manage billing history, and generate receipts.
5
+ *
6
+ * This module ensures comprehensive handling of billing data and operations, facilitating integrations with various payment providers and internal systems. It serves as a foundation for the financial transaction processing system, providing traceability and accountability through structured data and services.
7
+ *
8
+ * @module
9
+ */
10
+
11
+ /**
12
+ * Enum representing different types of payment methods.
13
+ * These are used to specify the payment mechanism chosen by a customer.
14
+ */
15
+ export enum PaymentMethod {
16
+ /** Payment through credit card. */
17
+ CreditCard = "Credit Card",
18
+
19
+ /** Payment through PayPal. Ideal for users who prefer not to directly use their credit card details. */
20
+ PayPal = "PayPal",
21
+
22
+ /** Payment via direct bank transfer. Used typically for larger transactions or where credit cards and PayPal are not viable. */
23
+ BankTransfer = "Bank Transfer",
24
+ }
25
+
26
+ /**
27
+ * Enum representing different transaction statuses.
28
+ * These statuses indicate the current state of a billing transaction in the process pipeline.
29
+ */
30
+ export enum TransactionStatus {
31
+ /** Transaction has been initiated but not yet processed. */
32
+ Pending = "Pending",
33
+
34
+ /** Transaction has been successfully processed. */
35
+ Completed = "Completed",
36
+
37
+ /** Transaction failed due to an error or rejection. */
38
+ Failed = "Failed",
39
+ }
40
+
41
+ /**
42
+ * Interface representing a billing transaction.
43
+ * This structure encapsulates all key details of a financial transaction within the system.
44
+ */
45
+ export interface BillingTransaction {
46
+ /**
47
+ * Unique identifier for the transaction. This ID is used to track and reference the transaction
48
+ * throughout the billing process.
49
+ */
50
+ transactionId: string;
51
+
52
+ /**
53
+ * Identifier for the order associated with this transaction. Helps link the transaction to specific goods or services purchased.
54
+ */
55
+ orderId: string;
56
+
57
+ /**
58
+ * The total amount of money involved in the transaction. This is usually in the smallest unit of the currency,
59
+ * such as cents in USD.
60
+ */
61
+ amount: number;
62
+
63
+ /**
64
+ * The method of payment used for the transaction, as defined by the PaymentMethod enum.
65
+ * This indicates how the customer chose to pay (e.g., Credit Card, PayPal, Bank Transfer).
66
+ */
67
+ paymentMethod: PaymentMethod;
68
+
69
+ /**
70
+ * The current status of the transaction, categorized by the TransactionStatus enum. Indicates whether the
71
+ * transaction is pending, completed, or has failed.
72
+ */
73
+ status: TransactionStatus;
74
+ }
75
+
76
+ /**
77
+ * Interface representing a billing receipt.
78
+ * Provides a record of the transaction for both the customer and the business.
79
+ */
80
+ export interface BillingReceipt {
81
+ /**
82
+ * Unique identifier for the receipt. This ID is crucial for referencing and tracking the receipt
83
+ * in financial records and customer queries.
84
+ */
85
+ receiptId: string;
86
+
87
+ /**
88
+ * The transaction details associated with this receipt. Links the receipt to the actual transaction
89
+ * that took place.
90
+ */
91
+ transaction: BillingTransaction;
92
+
93
+ /**
94
+ * The timestamp (in UNIX epoch time) when the receipt was generated. Provides a precise record of when
95
+ * the transaction was finalized and acknowledged.
96
+ */
97
+ timestamp: number;
98
+ }
99
+
100
+ /**
101
+ * Service for managing billing transactions.
102
+ * Provides functionality to process and retrieve billing transactions.
103
+ * @class
104
+ */
105
+ export class BillingService {
106
+ /** Holds all processed transactions within the service. */
107
+ private transactions: BillingTransaction[] = [];
108
+
109
+ /**
110
+ * Processes a billing transaction by adding it to the list of transactions.
111
+ * This simulates the transaction execution and storage in a production environment.
112
+ * @param transaction - The billing transaction to be processed.
113
+ * @returns The processed billing transaction, now stored in the service.
114
+ */
115
+ processTransaction(transaction: BillingTransaction): BillingTransaction {
116
+ this.transactions.push(transaction);
117
+ return transaction;
118
+ }
119
+
120
+ /**
121
+ * Retrieves all the billing transactions that have been processed.
122
+ * Useful for audits and general transaction management.
123
+ * @returns An array of all billing transactions.
124
+ */
125
+ getAllTransactions(): BillingTransaction[] {
126
+ return this.transactions;
127
+ }
128
+ }
129
+
130
+ /**
131
+ * Service for managing billing history.
132
+ * Provides functionality to store and retrieve the history of transactions.
133
+ * @class
134
+ */
135
+ export class BillingHistoryService {
136
+ /** Holds all transactions that have been added to the billing history. */
137
+ private history: BillingTransaction[] = [];
138
+
139
+ /**
140
+ * Adds a transaction to the billing history.
141
+ * This method is typically called after a transaction is completed to maintain a record.
142
+ * @param transaction - The billing transaction to be added to the history.
143
+ */
144
+ addTransactionToHistory(transaction: BillingTransaction): void {
145
+ this.history.push(transaction);
146
+ }
147
+
148
+ /**
149
+ * Retrieves the complete billing history, providing access to all transactions that have been recorded.
150
+ * This is useful for historical analysis and auditing purposes.
151
+ * @returns An array of all billing transactions in the history.
152
+ */
153
+ getBillingHistory(): BillingTransaction[] {
154
+ return this.history;
155
+ }
156
+ }
157
+
158
+ /**
159
+ * Service for generating billing receipts.
160
+ * Provides functionality to generate and retrieve receipts for transactions.
161
+ * @class
162
+ */
163
+ export class BillingReceiptService {
164
+ /**
165
+ * Stores all receipts generated for transactions.
166
+ */
167
+ public receipts: BillingReceipt[] = [];
168
+
169
+ /**
170
+ * Generates a billing receipt for a transaction and stores it.
171
+ * Each receipt includes a unique ID and timestamp, essential for record-keeping and customer service.
172
+ * @param transaction - The billing transaction for which to generate a receipt.
173
+ * @returns The generated billing receipt.
174
+ */
175
+ generateReceipt(transaction: BillingTransaction): BillingReceipt {
176
+ const receipt: BillingReceipt = {
177
+ receiptId: `receipt_${Date.now()}`,
178
+ transaction,
179
+ timestamp: Date.now(),
180
+ };
181
+ this.receipts.push(receipt);
182
+ return receipt;
183
+ }
184
+
185
+ /**
186
+ * Retrieves all the billing receipts that have been generated.
187
+ * Useful for providing customers with copies of their receipts or for internal financial tracking.
188
+ * @returns An array of all billing receipts.
189
+ */
190
+ getAllReceipts(): BillingReceipt[] {
191
+ return this.receipts;
192
+ }
193
+ }
@@ -0,0 +1,8 @@
1
+ export * from "./billing"
2
+
3
+ /**
4
+ * Returns a message builder message.
5
+ */
6
+ export function messageBuilder(): string {
7
+ return 'Hello world!';
8
+ }
@@ -0,0 +1,20 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "strict": true,
7
+ "esModuleInterop": true,
8
+ "skipLibCheck": true,
9
+ "forceConsistentCasingInFileNames": true,
10
+ "outDir": "./dist",
11
+ "declaration": true,
12
+ "declarationMap": true,
13
+ "incremental": true,
14
+ "tsBuildInfoFile": "./dist/tsconfig.tsbuildinfo"
15
+ },
16
+ "include": [
17
+ "src/**/*.ts"
18
+ ],
19
+ "exclude": ["node_modules", "dist"]
20
+ }
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@xyd-js/sources",
3
+ "version": "0.0.1-xyd.0",
4
+ "description": "",
5
+ "main": "./dist/index.js",
6
+ "type": "module",
7
+ "dependencies": {
8
+ "typedoc": "^0.28.1"
9
+ },
10
+ "peerDependencies": {
11
+ "@xyd-js/uniform": "0.1.0-xyd.11"
12
+ },
13
+ "devDependencies": {
14
+ "rimraf": "^3.0.2",
15
+ "tsup": "^8.3.0"
16
+ },
17
+ "scripts": {
18
+ "clean": "rimraf build",
19
+ "prebuild": "pnpm clean",
20
+ "build": "tsup"
21
+ }
22
+ }
@@ -0,0 +1,220 @@
1
+ [
2
+ {
3
+ "title": "Class ExampleClass",
4
+ "canonical": "class-ExampleClass",
5
+ "description": "---\ntitle: ExampleClass \ngroup: [Source Code, Classes]\n---\nReturns a personalized hello world message.\n",
6
+ "context": {
7
+ "fileName": "index.ts",
8
+ "fileFullPath": "src/index.ts",
9
+ "line": 32,
10
+ "col": 13,
11
+ "signatureText": {
12
+ "code": "export class ExampleClass {\n}",
13
+ "lang": "ts"
14
+ },
15
+ "sourcecode": {
16
+ "code": "export class ExampleClass {\n /**\n * Returns a hello world message.\n */\n helloWorld(): string {\n return 'Hello world!';\n }\n}",
17
+ "lang": "ts"
18
+ },
19
+ "package": "@xyd-sources-examples/package-a"
20
+ },
21
+ "examples": {
22
+ "groups": []
23
+ },
24
+ "definitions": [
25
+ {
26
+ "title": "Constructor",
27
+ "properties": []
28
+ },
29
+ {
30
+ "title": "Methods",
31
+ "properties": [
32
+ {
33
+ "name": "helloWorld",
34
+ "type": "string",
35
+ "description": "Returns a hello world message.\n"
36
+ }
37
+ ]
38
+ }
39
+ ]
40
+ },
41
+ {
42
+ "title": "Function gqlSchemaToReferences",
43
+ "canonical": "fn-gqlSchemaToReferences",
44
+ "description": "---\ntitle: gqlSchemaToReferences\nabc: 123\ngroup: [Sourc, Functions]\n---\nConverts a GraphQL schema file to references.\n",
45
+ "context": {
46
+ "fileName": "index.ts",
47
+ "fileFullPath": "src/index.ts",
48
+ "line": 48,
49
+ "col": 16,
50
+ "signatureText": {
51
+ "code": "export function gqlSchemaToReferences(schemaLocation: string): Promise<[\n]>;",
52
+ "lang": "ts"
53
+ },
54
+ "sourcecode": {
55
+ "code": "export function gqlSchemaToReferences(\n schemaLocation: string\n): Promise<[]> {\n if (schemaLocation.endsWith(\".graphql\")) {\n return Promise.resolve([])\n }\n\n return Promise.resolve([])\n}",
56
+ "lang": "ts"
57
+ },
58
+ "package": "@xyd-sources-examples/package-a"
59
+ },
60
+ "examples": {
61
+ "groups": []
62
+ },
63
+ "definitions": [
64
+ {
65
+ "title": "Returns",
66
+ "properties": [
67
+ {
68
+ "name": "",
69
+ "type": "<Promise>",
70
+ "description": "references\n"
71
+ }
72
+ ]
73
+ },
74
+ {
75
+ "title": "Parameters",
76
+ "properties": [
77
+ {
78
+ "name": "schemaLocation",
79
+ "type": "string",
80
+ "description": "The location of the schema file.\n"
81
+ }
82
+ ]
83
+ }
84
+ ]
85
+ },
86
+ {
87
+ "title": "Function helloWorld",
88
+ "canonical": "fn-helloWorld",
89
+ "description": "---\ntitle: helloWorld\nabc: 123\ngroup: [Sourc, Functions]\n---\nReturns a hello world message.\n",
90
+ "context": {
91
+ "fileName": "index.ts",
92
+ "fileFullPath": "src/index.ts",
93
+ "line": 4,
94
+ "col": 16,
95
+ "signatureText": {
96
+ "code": "export function helloWorld(): string;",
97
+ "lang": "ts"
98
+ },
99
+ "sourcecode": {
100
+ "code": "export function helloWorld(): string {\n return 'Hello world!';\n}",
101
+ "lang": "ts"
102
+ },
103
+ "package": "@xyd-sources-examples/package-a"
104
+ },
105
+ "examples": {
106
+ "groups": []
107
+ },
108
+ "definitions": [
109
+ {
110
+ "title": "Returns",
111
+ "properties": [
112
+ {
113
+ "name": "",
114
+ "type": "string",
115
+ "description": ""
116
+ }
117
+ ]
118
+ },
119
+ {
120
+ "title": "Parameters",
121
+ "properties": []
122
+ }
123
+ ]
124
+ },
125
+ {
126
+ "title": "Function helloWorldV2",
127
+ "canonical": "fn-helloWorldV2",
128
+ "description": "---\ntitle: helloWorldV2\nabc: 123\ngroup: [Sourc, Functions]\n---\nReturns a personalized hello world message.\n",
129
+ "context": {
130
+ "fileName": "index.ts",
131
+ "fileFullPath": "src/index.ts",
132
+ "line": 15,
133
+ "col": 16,
134
+ "signatureText": {
135
+ "code": "export function helloWorldV2(name: string, enthusiastic: boolean = false): string;",
136
+ "lang": "ts"
137
+ },
138
+ "sourcecode": {
139
+ "code": "export function helloWorldV2(name: string, enthusiastic: boolean = false): string {\n return enthusiastic ? `Hello, ${name}!` : `Hello, ${name}`;\n}",
140
+ "lang": "ts"
141
+ },
142
+ "package": "@xyd-sources-examples/package-a"
143
+ },
144
+ "examples": {
145
+ "groups": []
146
+ },
147
+ "definitions": [
148
+ {
149
+ "title": "Returns",
150
+ "properties": [
151
+ {
152
+ "name": "",
153
+ "type": "string",
154
+ "description": "A greeting message string.\n"
155
+ }
156
+ ]
157
+ },
158
+ {
159
+ "title": "Parameters",
160
+ "properties": [
161
+ {
162
+ "name": "name",
163
+ "type": "string",
164
+ "description": "Name of the person to greet.\n"
165
+ },
166
+ {
167
+ "name": "enthusiastic",
168
+ "type": "boolean",
169
+ "description": "If true, adds an exclamation point to the greeting.\n"
170
+ }
171
+ ]
172
+ }
173
+ ]
174
+ },
175
+ {
176
+ "title": "Function helloWorldV3",
177
+ "canonical": "fn-helloWorldV3",
178
+ "description": "---\ntitle: helloWorldV3\nabc: 123\ngroup: [Sourc, Functions]\n---\nReturns a personalized hello world message.\n",
179
+ "context": {
180
+ "fileName": "index.ts",
181
+ "fileFullPath": "src/index.ts",
182
+ "line": 25,
183
+ "col": 16,
184
+ "signatureText": {
185
+ "code": "export function helloWorldV3<T>(name: T): string;",
186
+ "lang": "ts"
187
+ },
188
+ "sourcecode": {
189
+ "code": "export function helloWorldV3<T>(name: T): string {\n return `Hello, ${name}!`\n}",
190
+ "lang": "ts"
191
+ },
192
+ "package": "@xyd-sources-examples/package-a"
193
+ },
194
+ "examples": {
195
+ "groups": []
196
+ },
197
+ "definitions": [
198
+ {
199
+ "title": "Returns",
200
+ "properties": [
201
+ {
202
+ "name": "",
203
+ "type": "string",
204
+ "description": "A greeting message string.\n"
205
+ }
206
+ ]
207
+ },
208
+ {
209
+ "title": "Parameters",
210
+ "properties": [
211
+ {
212
+ "name": "name",
213
+ "type": "<T>",
214
+ "description": "Name of the person to greet.\n"
215
+ }
216
+ ]
217
+ }
218
+ ]
219
+ }
220
+ ]
@@ -0,0 +1,214 @@
1
+ import * as ts from 'typescript';
2
+ import * as fs from "node:fs";
3
+
4
+ // TODO: in the future more lightweight solution ??? - currently it's fine cuz it uses battle-tested TypeScript API
5
+
6
+ const printer = ts.createPrinter({removeComments: true});
7
+
8
+ export class SignatureTextLoader {
9
+ protected sourceFile: ts.SourceFile;
10
+
11
+ constructor(sourcePath: string) {
12
+ const source = fs.readFileSync(sourcePath, 'utf-8');
13
+
14
+ this.sourceFile = ts.createSourceFile(
15
+ sourcePath,
16
+ source,
17
+ ts.ScriptTarget.Latest,
18
+ true
19
+ );
20
+ }
21
+ }
22
+
23
+ export class MultiSignatureLoader {
24
+ private loaders: Map<string, SignatureTextLoader>
25
+
26
+ constructor() {
27
+ this.loaders = new Map<string, SignatureTextLoader>()
28
+ }
29
+
30
+ protected load(path: string) {
31
+ if (this.loaders.has(path)) {
32
+ return this.loaders.get(path)
33
+ }
34
+
35
+ const loader = new SignatureTextLoader(path)
36
+ this.loaders.set(path, loader)
37
+
38
+ return loader
39
+ }
40
+ }
41
+
42
+ /**
43
+ * Get the signature text of a function, class, interface, enum, or type alias at a specific line.
44
+ *
45
+ * @param sign - instance of SignatureText
46
+ * @param targetLine - the line number of the signature in source code
47
+ *
48
+ * @returns code friendly signature text
49
+ */
50
+ export function signatureTextByLine(
51
+ sign: SignatureTextLoader,
52
+ targetLine: number
53
+ ) {
54
+ return signatureText.call(sign, targetLine)
55
+ }
56
+
57
+ /**
58
+ * Get the source code of a function, class, interface, enum, or type alias at a specific line.
59
+ *
60
+ * @param sign - instance of SignatureText
61
+ * @param targetLine - the line number of the signature in source code
62
+ *
63
+ * @returns source code of the signature
64
+ */
65
+ export function signatureSourceCodeByLine(
66
+ sign: SignatureTextLoader,
67
+ targetLine: number
68
+ ) {
69
+ return signatureSourceCode.call(sign, targetLine)
70
+ }
71
+
72
+ function signatureText(
73
+ this: SignatureTextLoader,
74
+ targetLine: number
75
+ ) {
76
+ const sourceFile = this.sourceFile;
77
+ const signatureNode = findSignatureNode.call(
78
+ this,
79
+ sourceFile,
80
+ [targetLine]
81
+ );
82
+
83
+ if (!signatureNode) {
84
+ console.error("(signatureText): `signatureNode` is empty, something went wrong");
85
+ return
86
+ }
87
+
88
+ const printableSignatureNode = nodeToPrintableSignatureNode(signatureNode);
89
+ if (!printableSignatureNode) {
90
+ console.error("(signatureText): cannot convert `signatureNode` to `printableSignatureNode`");
91
+ return
92
+ }
93
+
94
+ return printer.printNode(ts.EmitHint.Unspecified, printableSignatureNode, sourceFile).trim()
95
+
96
+ }
97
+
98
+ // TODO: this function is probably not optimized well (recursion when not needed)
99
+ function findSignatureNode(
100
+ this: SignatureTextLoader,
101
+ node: ts.Node,
102
+ targetLines: number[]
103
+ ) {
104
+ let isSourceFile = false
105
+ if (node === node.getSourceFile()) {
106
+ isSourceFile = true
107
+ }
108
+
109
+ if (!isSourceFile && isNodeAtLine(node, targetLines, this.sourceFile)) {
110
+ return node
111
+ }
112
+
113
+ let signatureNode: ts.Node | undefined;
114
+
115
+ ts.forEachChild(node, (n) => {
116
+ if (signatureNode) {
117
+ return
118
+ }
119
+ signatureNode = findSignatureNode.call(this, n, targetLines)
120
+ });
121
+
122
+ return signatureNode
123
+ }
124
+
125
+
126
+ function signatureSourceCode(
127
+ this: SignatureTextLoader,
128
+ targetLine: number
129
+ ) {
130
+ const sourceFile = this.sourceFile;
131
+ const signatureNode = findSignatureNode.call(
132
+ this,
133
+ sourceFile,
134
+ [targetLine]
135
+ );
136
+
137
+ if (!signatureNode) {
138
+ console.error("(signatureSourceCode): `signatureNode` is empty, something went wrong");
139
+ return
140
+ }
141
+
142
+ // Get the start and end positions of the node
143
+ const start = signatureNode.getStart(sourceFile);
144
+ const end = signatureNode.getEnd();
145
+
146
+ // Get the source text between start and end
147
+ return sourceFile.text.substring(start, end).trim();
148
+ }
149
+
150
+ function nodeToPrintableSignatureNode(node: ts.Node) {
151
+ let resp: ts.Node | undefined;
152
+
153
+ if (ts.isFunctionDeclaration(node)) {
154
+ resp = ts.factory.updateFunctionDeclaration(
155
+ node,
156
+ node.modifiers,
157
+ node.asteriskToken,
158
+ node.name,
159
+ node.typeParameters,
160
+ node.parameters,
161
+ node.type,
162
+ undefined
163
+ );
164
+ } else if (ts.isClassDeclaration(node)) {
165
+ resp = ts.factory.updateClassDeclaration(
166
+ node,
167
+ node.modifiers,
168
+ node.name,
169
+ node.typeParameters,
170
+ node.heritageClauses,
171
+ []
172
+ );
173
+ } else if (ts.isInterfaceDeclaration(node)) {
174
+ resp = ts.factory.updateInterfaceDeclaration(
175
+ node,
176
+ node.modifiers,
177
+ node.name,
178
+ node.typeParameters,
179
+ node.heritageClauses,
180
+ []
181
+ );
182
+ } else if (ts.isEnumDeclaration(node)) {
183
+ resp = ts.factory.updateEnumDeclaration(
184
+ node,
185
+ node.modifiers,
186
+ node.name,
187
+ []
188
+ );
189
+ } else if (ts.isTypeAliasDeclaration(node)) {
190
+ resp = ts.factory.updateTypeAliasDeclaration(
191
+ node,
192
+ node.modifiers,
193
+ node.name,
194
+ node.typeParameters,
195
+ node.type
196
+ );
197
+ }
198
+
199
+ if (!resp) {
200
+ console.error("(nodeToPrintableSignatureNode): resp is empty, something went wrong");
201
+ return;
202
+ }
203
+
204
+ return resp
205
+ }
206
+
207
+ function isNodeAtLine(node: ts.Node, lines: number[], sf: ts.SourceFile): boolean {
208
+ const {line: startLine} = sf.getLineAndCharacterOfPosition(node.getStart());
209
+
210
+ return lines.includes(startLine + 1); // lines are 0-based internally
211
+ }
212
+
213
+
214
+