isahaq-barcode 1.0.0 → 1.1.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.
@@ -1,159 +1,163 @@
1
- /**
2
- * Express.js server example for Isahaq Barcode Generator
3
- */
4
-
5
- const express = require('express');
6
- const BarcodeGenerator = require('../index');
7
-
8
- const app = express();
9
- const PORT = process.env.PORT || 3000;
10
-
11
- // Middleware
12
- app.use(express.json());
13
- app.use(express.static('public'));
14
-
15
- // Barcode generation endpoint
16
- app.get('/barcode/:data', async (req, res) => {
17
- const { data } = req.params;
18
- const { type = 'code128', format = 'png', width, height } = req.query;
19
-
20
- try {
21
- const options = {};
22
- if (width) options.width = parseInt(width);
23
- if (height) options.height = parseInt(height);
24
-
25
- let result;
26
- let contentType;
27
-
28
- switch (format) {
29
- case 'png':
30
- result = BarcodeGenerator.png(data, type, options);
31
- contentType = 'image/png';
32
- break;
33
- case 'svg':
34
- result = BarcodeGenerator.svg(data, type, options);
35
- contentType = 'image/svg+xml';
36
- break;
37
- case 'html':
38
- result = BarcodeGenerator.html(data, type, options);
39
- contentType = 'text/html';
40
- break;
41
- case 'pdf':
42
- result = await BarcodeGenerator.pdf(data, type, options);
43
- contentType = 'application/pdf';
44
- break;
45
- default:
46
- throw new Error(`Unsupported format: ${format}`);
47
- }
48
-
49
- res.set('Content-Type', contentType);
50
- res.send(result);
51
- } catch (error) {
52
- res.status(400).json({ error: error.message });
53
- }
54
- });
55
-
56
- // QR code generation endpoint
57
- app.get('/qr/:data', async (req, res) => {
58
- const { data } = req.params;
59
- const {
60
- size = 300,
61
- logo,
62
- label,
63
- watermark,
64
- watermarkPosition = 'center',
65
- errorCorrection = 'M'
66
- } = req.query;
67
-
68
- try {
69
- const qrOptions = {
70
- data: decodeURIComponent(data),
71
- size: parseInt(size),
72
- errorCorrectionLevel: errorCorrection
73
- };
74
-
75
- if (logo) qrOptions.logoPath = logo;
76
- if (label) qrOptions.label = label;
77
- if (watermark) {
78
- qrOptions.watermark = watermark;
79
- qrOptions.watermarkPosition = watermarkPosition;
80
- }
81
-
82
- const qrCode = BarcodeGenerator.modernQr(qrOptions);
83
- const result = await qrCode.generate();
84
-
85
- res.set('Content-Type', 'image/png');
86
- res.send(result);
87
- } catch (error) {
88
- res.status(400).json({ error: error.message });
89
- }
90
- });
91
-
92
- // Batch generation endpoint
93
- app.post('/batch', async (req, res) => {
94
- try {
95
- const { items } = req.body;
96
-
97
- if (!Array.isArray(items)) {
98
- throw new Error('Items must be an array');
99
- }
100
-
101
- const results = BarcodeGenerator.batch(items);
102
- res.json({ results });
103
- } catch (error) {
104
- res.status(400).json({ error: error.message });
105
- }
106
- });
107
-
108
- // Validation endpoint
109
- app.get('/validate/:data/:type', (req, res) => {
110
- const { data, type } = req.params;
111
-
112
- try {
113
- const validation = BarcodeGenerator.validate(data, type);
114
- res.json(validation);
115
- } catch (error) {
116
- res.status(400).json({ error: error.message });
117
- }
118
- });
119
-
120
- // Get supported types
121
- app.get('/types', (req, res) => {
122
- const types = BarcodeGenerator.getBarcodeTypes();
123
- res.json({ types });
124
- });
125
-
126
- // Get supported formats
127
- app.get('/formats', (req, res) => {
128
- const formats = BarcodeGenerator.getRenderFormats();
129
- res.json({ formats });
130
- });
131
-
132
- // Get watermark positions
133
- app.get('/watermark-positions', (req, res) => {
134
- const positions = BarcodeGenerator.getWatermarkPositions();
135
- res.json({ positions });
136
- });
137
-
138
- // Health check
139
- app.get('/health', (req, res) => {
140
- res.json({ status: 'OK', timestamp: new Date().toISOString() });
141
- });
142
-
143
- // Error handling middleware
144
- app.use((error, req, res) => {
145
- console.error('Error:', error);
146
- res.status(500).json({ error: 'Internal server error' });
147
- });
148
-
149
- // Start server
150
- app.listen(PORT, () => {
151
- console.log(`🚀 Server running on http://localhost:${PORT}`);
152
- console.log(`📊 Health check: http://localhost:${PORT}/health`);
153
- console.log(`🔗 Barcode endpoint: http://localhost:${PORT}/barcode/{data}?type=code128&format=png`);
154
- console.log(`🔗 QR code endpoint: http://localhost:${PORT}/qr/{data}?size=300`);
155
- console.log(`🔗 Types endpoint: http://localhost:${PORT}/types`);
156
- console.log(`🔗 Formats endpoint: http://localhost:${PORT}/formats`);
157
- });
158
-
159
- module.exports = app;
1
+ /**
2
+ * Express.js server example for Isahaq Barcode Generator
3
+ */
4
+
5
+ const express = require('express');
6
+ const BarcodeGenerator = require('../index');
7
+
8
+ const app = express();
9
+ const PORT = process.env.PORT || 3000;
10
+
11
+ // Middleware
12
+ app.use(express.json());
13
+ app.use(express.static('public'));
14
+
15
+ // Barcode generation endpoint
16
+ app.get('/barcode/:data', async (req, res) => {
17
+ const { data } = req.params;
18
+ const { type = 'code128', format = 'png', width, height } = req.query;
19
+
20
+ try {
21
+ const options = {};
22
+ if (width) options.width = parseInt(width);
23
+ if (height) options.height = parseInt(height);
24
+
25
+ let result;
26
+ let contentType;
27
+
28
+ switch (format) {
29
+ case 'png':
30
+ result = BarcodeGenerator.png(data, type, options);
31
+ contentType = 'image/png';
32
+ break;
33
+ case 'svg':
34
+ result = BarcodeGenerator.svg(data, type, options);
35
+ contentType = 'image/svg+xml';
36
+ break;
37
+ case 'html':
38
+ result = BarcodeGenerator.html(data, type, options);
39
+ contentType = 'text/html';
40
+ break;
41
+ case 'pdf':
42
+ result = await BarcodeGenerator.pdf(data, type, options);
43
+ contentType = 'application/pdf';
44
+ break;
45
+ default:
46
+ throw new Error(`Unsupported format: ${format}`);
47
+ }
48
+
49
+ res.set('Content-Type', contentType);
50
+ res.send(result);
51
+ } catch (error) {
52
+ res.status(400).json({ error: error.message });
53
+ }
54
+ });
55
+
56
+ // QR code generation endpoint
57
+ app.get('/qr/:data', async (req, res) => {
58
+ const { data } = req.params;
59
+ const {
60
+ size = 300,
61
+ logo,
62
+ label,
63
+ watermark,
64
+ watermarkPosition = 'center',
65
+ errorCorrection = 'M',
66
+ } = req.query;
67
+
68
+ try {
69
+ const qrOptions = {
70
+ data: decodeURIComponent(data),
71
+ size: parseInt(size),
72
+ errorCorrectionLevel: errorCorrection,
73
+ };
74
+
75
+ if (logo) qrOptions.logoPath = logo;
76
+ if (label) qrOptions.label = label;
77
+ if (watermark) {
78
+ qrOptions.watermark = watermark;
79
+ qrOptions.watermarkPosition = watermarkPosition;
80
+ }
81
+
82
+ const qrCode = BarcodeGenerator.modernQr(qrOptions);
83
+ const result = await qrCode.generate();
84
+
85
+ res.set('Content-Type', 'image/png');
86
+ res.send(result);
87
+ } catch (error) {
88
+ res.status(400).json({ error: error.message });
89
+ }
90
+ });
91
+
92
+ // Batch generation endpoint
93
+ app.post('/batch', async (req, res) => {
94
+ try {
95
+ const { items } = req.body;
96
+
97
+ if (!Array.isArray(items)) {
98
+ throw new Error('Items must be an array');
99
+ }
100
+
101
+ const results = BarcodeGenerator.batch(items);
102
+ res.json({ results });
103
+ } catch (error) {
104
+ res.status(400).json({ error: error.message });
105
+ }
106
+ });
107
+
108
+ // Validation endpoint
109
+ app.get('/validate/:data/:type', (req, res) => {
110
+ const { data, type } = req.params;
111
+
112
+ try {
113
+ const validation = BarcodeGenerator.validate(data, type);
114
+ res.json(validation);
115
+ } catch (error) {
116
+ res.status(400).json({ error: error.message });
117
+ }
118
+ });
119
+
120
+ // Get supported types
121
+ app.get('/types', (req, res) => {
122
+ const types = BarcodeGenerator.getBarcodeTypes();
123
+ res.json({ types });
124
+ });
125
+
126
+ // Get supported formats
127
+ app.get('/formats', (req, res) => {
128
+ const formats = BarcodeGenerator.getRenderFormats();
129
+ res.json({ formats });
130
+ });
131
+
132
+ // Get watermark positions
133
+ app.get('/watermark-positions', (req, res) => {
134
+ const positions = BarcodeGenerator.getWatermarkPositions();
135
+ res.json({ positions });
136
+ });
137
+
138
+ // Health check
139
+ app.get('/health', (req, res) => {
140
+ res.json({ status: 'OK', timestamp: new Date().toISOString() });
141
+ });
142
+
143
+ // Error handling middleware
144
+ app.use((error, req, res) => {
145
+ console.error('Error:', error);
146
+ res.status(500).json({ error: 'Internal server error' });
147
+ });
148
+
149
+ // Start server
150
+ app.listen(PORT, () => {
151
+ console.log(`🚀 Server running on http://localhost:${PORT}`);
152
+ console.log(`📊 Health check: http://localhost:${PORT}/health`);
153
+ console.log(
154
+ `🔗 Barcode endpoint: http://localhost:${PORT}/barcode/{data}?type=code128&format=png`
155
+ );
156
+ console.log(
157
+ `🔗 QR code endpoint: http://localhost:${PORT}/qr/{data}?size=300`
158
+ );
159
+ console.log(`🔗 Types endpoint: http://localhost:${PORT}/types`);
160
+ console.log(`🔗 Formats endpoint: http://localhost:${PORT}/formats`);
161
+ });
162
+
163
+ module.exports = app;
package/index.js CHANGED
@@ -1,134 +1,134 @@
1
- /**
2
- * Isahaq Barcode Generator
3
- * A universal barcode generator package supporting multiple barcode types and output formats
4
- */
5
-
6
- const BarcodeService = require('./src/services/BarcodeService');
7
- const QrCodeBuilder = require('./src/builders/QrCodeBuilder');
8
- const { BarcodeTypes } = require('./src/types/BarcodeTypes');
9
- const { RenderFormats } = require('./src/renderers/RenderFormats');
10
-
11
- // Main service instance
12
- const barcodeService = new BarcodeService();
13
-
14
- /**
15
- * Main Barcode Generator Class
16
- */
17
- class BarcodeGenerator {
18
- /**
19
- * Generate a barcode in PNG format
20
- * @param {string} data - The data to encode
21
- * @param {string} type - The barcode type
22
- * @param {Object} options - Additional options
23
- * @returns {Buffer} PNG buffer
24
- */
25
- static png(data, type = 'code128', options = {}) {
26
- return barcodeService.generate(data, type, 'png', options);
27
- }
28
-
29
- /**
30
- * Generate a barcode in SVG format
31
- * @param {string} data - The data to encode
32
- * @param {string} type - The barcode type
33
- * @param {Object} options - Additional options
34
- * @returns {string} SVG string
35
- */
36
- static svg(data, type = 'code128', options = {}) {
37
- return barcodeService.generate(data, type, 'svg', options);
38
- }
39
-
40
- /**
41
- * Generate a barcode in HTML format
42
- * @param {string} data - The data to encode
43
- * @param {string} type - The barcode type
44
- * @param {Object} options - Additional options
45
- * @returns {string} HTML string
46
- */
47
- static html(data, type = 'code128', options = {}) {
48
- return barcodeService.generate(data, type, 'html', options);
49
- }
50
-
51
- /**
52
- * Generate a barcode in PDF format
53
- * @param {string} data - The data to encode
54
- * @param {string} type - The barcode type
55
- * @param {Object} options - Additional options
56
- * @returns {Buffer} PDF buffer
57
- */
58
- static pdf(data, type = 'code128', options = {}) {
59
- return barcodeService.generate(data, type, 'pdf', options);
60
- }
61
-
62
- /**
63
- * Generate a QR code with advanced features
64
- * @param {Object} options - QR code options
65
- * @returns {Object} QR code result
66
- */
67
- static modernQr(options = {}) {
68
- return QrCodeBuilder.create(options);
69
- }
70
-
71
- /**
72
- * Get available barcode types
73
- * @returns {Array} Array of barcode types
74
- */
75
- static getBarcodeTypes() {
76
- return BarcodeTypes.getAll();
77
- }
78
-
79
- /**
80
- * Get available render formats
81
- * @returns {Array} Array of render formats
82
- */
83
- static getRenderFormats() {
84
- return RenderFormats.getAll();
85
- }
86
-
87
- /**
88
- * Validate data for a specific barcode type
89
- * @param {string} data - The data to validate
90
- * @param {string} type - The barcode type
91
- * @returns {Object} Validation result
92
- */
93
- static validate(data, type) {
94
- return barcodeService.validate(data, type);
95
- }
96
-
97
- /**
98
- * Generate multiple barcodes in batch
99
- * @param {Array} items - Array of barcode generation items
100
- * @returns {Array} Array of generated barcodes
101
- */
102
- static batch(items) {
103
- return barcodeService.batch(items);
104
- }
105
-
106
- /**
107
- * Get watermark positions for QR codes
108
- * @returns {Array} Array of watermark positions
109
- */
110
- static getWatermarkPositions() {
111
- return [
112
- 'top-left',
113
- 'top-right',
114
- 'bottom-left',
115
- 'bottom-right',
116
- 'center',
117
- 'top-center',
118
- 'bottom-center',
119
- 'left-center',
120
- 'right-center',
121
- ];
122
- }
123
- }
124
-
125
- // Export main class and utilities
126
- module.exports = BarcodeGenerator;
127
- module.exports.BarcodeGenerator = BarcodeGenerator;
128
- module.exports.BarcodeService = BarcodeService;
129
- module.exports.QrCodeBuilder = QrCodeBuilder;
130
- module.exports.BarcodeTypes = BarcodeTypes;
131
- module.exports.RenderFormats = RenderFormats;
132
-
133
- // Default export
134
- module.exports.default = BarcodeGenerator;
1
+ /**
2
+ * Isahaq Barcode Generator
3
+ * A universal barcode generator package supporting multiple barcode types and output formats
4
+ */
5
+
6
+ const BarcodeService = require('./src/services/BarcodeService');
7
+ const QrCodeBuilder = require('./src/builders/QrCodeBuilder');
8
+ const { BarcodeTypes } = require('./src/types/BarcodeTypes');
9
+ const { RenderFormats } = require('./src/renderers/RenderFormats');
10
+
11
+ // Main service instance
12
+ const barcodeService = new BarcodeService();
13
+
14
+ /**
15
+ * Main Barcode Generator Class
16
+ */
17
+ class BarcodeGenerator {
18
+ /**
19
+ * Generate a barcode in PNG format
20
+ * @param {string} data - The data to encode
21
+ * @param {string} type - The barcode type
22
+ * @param {Object} options - Additional options
23
+ * @returns {Buffer} PNG buffer
24
+ */
25
+ static png(data, type = 'code128', options = {}) {
26
+ return barcodeService.generate(data, type, 'png', options);
27
+ }
28
+
29
+ /**
30
+ * Generate a barcode in SVG format
31
+ * @param {string} data - The data to encode
32
+ * @param {string} type - The barcode type
33
+ * @param {Object} options - Additional options
34
+ * @returns {string} SVG string
35
+ */
36
+ static svg(data, type = 'code128', options = {}) {
37
+ return barcodeService.generate(data, type, 'svg', options);
38
+ }
39
+
40
+ /**
41
+ * Generate a barcode in HTML format
42
+ * @param {string} data - The data to encode
43
+ * @param {string} type - The barcode type
44
+ * @param {Object} options - Additional options
45
+ * @returns {string} HTML string
46
+ */
47
+ static html(data, type = 'code128', options = {}) {
48
+ return barcodeService.generate(data, type, 'html', options);
49
+ }
50
+
51
+ /**
52
+ * Generate a barcode in PDF format
53
+ * @param {string} data - The data to encode
54
+ * @param {string} type - The barcode type
55
+ * @param {Object} options - Additional options
56
+ * @returns {Buffer} PDF buffer
57
+ */
58
+ static pdf(data, type = 'code128', options = {}) {
59
+ return barcodeService.generate(data, type, 'pdf', options);
60
+ }
61
+
62
+ /**
63
+ * Generate a QR code with advanced features
64
+ * @param {Object} options - QR code options
65
+ * @returns {Object} QR code result
66
+ */
67
+ static modernQr(options = {}) {
68
+ return QrCodeBuilder.create(options);
69
+ }
70
+
71
+ /**
72
+ * Get available barcode types
73
+ * @returns {Array} Array of barcode types
74
+ */
75
+ static getBarcodeTypes() {
76
+ return BarcodeTypes.getAll();
77
+ }
78
+
79
+ /**
80
+ * Get available render formats
81
+ * @returns {Array} Array of render formats
82
+ */
83
+ static getRenderFormats() {
84
+ return RenderFormats.getAll();
85
+ }
86
+
87
+ /**
88
+ * Validate data for a specific barcode type
89
+ * @param {string} data - The data to validate
90
+ * @param {string} type - The barcode type
91
+ * @returns {Object} Validation result
92
+ */
93
+ static validate(data, type) {
94
+ return barcodeService.validate(data, type);
95
+ }
96
+
97
+ /**
98
+ * Generate multiple barcodes in batch
99
+ * @param {Array} items - Array of barcode generation items
100
+ * @returns {Array} Array of generated barcodes
101
+ */
102
+ static batch(items) {
103
+ return barcodeService.batch(items);
104
+ }
105
+
106
+ /**
107
+ * Get watermark positions for QR codes
108
+ * @returns {Array} Array of watermark positions
109
+ */
110
+ static getWatermarkPositions() {
111
+ return [
112
+ 'top-left',
113
+ 'top-right',
114
+ 'bottom-left',
115
+ 'bottom-right',
116
+ 'center',
117
+ 'top-center',
118
+ 'bottom-center',
119
+ 'left-center',
120
+ 'right-center',
121
+ ];
122
+ }
123
+ }
124
+
125
+ // Export main class and utilities
126
+ module.exports = BarcodeGenerator;
127
+ module.exports.BarcodeGenerator = BarcodeGenerator;
128
+ module.exports.BarcodeService = BarcodeService;
129
+ module.exports.QrCodeBuilder = QrCodeBuilder;
130
+ module.exports.BarcodeTypes = BarcodeTypes;
131
+ module.exports.RenderFormats = RenderFormats;
132
+
133
+ // Default export
134
+ module.exports.default = BarcodeGenerator;
package/jest.config.js CHANGED
@@ -1,13 +1,13 @@
1
- module.exports = {
2
- testEnvironment: 'node',
3
- testMatch: ['**/__tests__/**/*.js', '**/?(*.)+(spec|test).js'],
4
- collectCoverageFrom: [
5
- 'src/**/*.js',
6
- '!src/**/*.test.js',
7
- '!src/**/__tests__/**',
8
- ],
9
- coverageDirectory: 'coverage',
10
- coverageReporters: ['text', 'lcov', 'html'],
11
- setupFilesAfterEnv: ['<rootDir>/tests/setup.js'],
12
- testTimeout: 10000,
13
- };
1
+ module.exports = {
2
+ testEnvironment: 'node',
3
+ testMatch: ['**/__tests__/**/*.js', '**/?(*.)+(spec|test).js'],
4
+ collectCoverageFrom: [
5
+ 'src/**/*.js',
6
+ '!src/**/*.test.js',
7
+ '!src/**/__tests__/**',
8
+ ],
9
+ coverageDirectory: 'coverage',
10
+ coverageReporters: ['text', 'lcov', 'html'],
11
+ setupFilesAfterEnv: ['<rootDir>/tests/setup.js'],
12
+ testTimeout: 10000,
13
+ };