@qr-platform/qr-code.js 0.11.4 → 0.11.8
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/docs/advanced-examples.md +269 -8
- package/docs/api-reference-guide.md +20 -12
- package/docs/documentation.md +501 -11
- package/docs/examples.md +127 -21
- package/docs/license-management.md +4 -13
- package/docs/typescript-types-definitions.md +198 -13
- package/docs/usage-guide.md +236 -65
- package/lib/index.js +1 -1
- package/lib/node.js +1 -1
- package/package.json +1 -1
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
---
|
|
2
|
+
title: 'Advanced Examples for QR-Code.js'
|
|
3
|
+
description: 'Advanced examples demonstrating the customization capabilities of QRCode.js'
|
|
4
|
+
---
|
|
3
5
|
|
|
4
6
|
This document provides advanced examples demonstrating the customization capabilities of QRCode.js. Each section focuses on specific options to help you create unique and visually appealing QR codes.
|
|
5
7
|
|
|
@@ -875,9 +877,268 @@ createAndValidate();
|
|
|
875
877
|
|
|
876
878
|
---
|
|
877
879
|
|
|
878
|
-
###
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
880
|
+
### Node.js Static Validation Methods (Premium Feature)
|
|
881
|
+
|
|
882
|
+
QRCode.js provides static validation methods specifically for Node.js environments to validate existing QR codes from image data or SVG strings.
|
|
883
|
+
|
|
884
|
+
**Example 1: Validating Image Data (Node.js Only)**
|
|
885
|
+
|
|
886
|
+
```typescript
|
|
887
|
+
// Node.js import
|
|
888
|
+
import { QRCodeJs } from '@qr-platform/qr-code.js/node';
|
|
889
|
+
import fs from 'fs';
|
|
890
|
+
|
|
891
|
+
// Ensure license is activated first
|
|
892
|
+
// await QRCodeJs.license('YOUR-LICENSE-KEY');
|
|
893
|
+
|
|
894
|
+
async function validateQRFromImage() {
|
|
895
|
+
try {
|
|
896
|
+
// Read image file as buffer
|
|
897
|
+
const imageBuffer = fs.readFileSync('path/to/qr-code-image.png');
|
|
898
|
+
|
|
899
|
+
// Validate the QR code from image data
|
|
900
|
+
const result = await QRCodeJs.validateImageData(imageBuffer);
|
|
901
|
+
|
|
902
|
+
if (result.isValid) {
|
|
903
|
+
console.log(`QR code is valid! Decoded text: ${result.data}`);
|
|
904
|
+
console.log(`Validator used: ${result.validator}`);
|
|
905
|
+
} else {
|
|
906
|
+
console.warn(`QR code validation failed: ${result.message}`);
|
|
907
|
+
}
|
|
908
|
+
} catch (error) {
|
|
909
|
+
console.error('Error validating image:', error);
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
validateQRFromImage();
|
|
914
|
+
```
|
|
915
|
+
|
|
916
|
+
**Example 2: Validating SVG Strings (Node.js Only)**
|
|
917
|
+
|
|
918
|
+
```typescript
|
|
919
|
+
// Node.js import
|
|
920
|
+
import { QRCodeJs } from '@qr-platform/qr-code.js/node';
|
|
921
|
+
|
|
922
|
+
// Ensure license is activated first
|
|
923
|
+
// await QRCodeJs.license('YOUR-LICENSE-KEY');
|
|
924
|
+
|
|
925
|
+
async function validateQRFromSVG() {
|
|
926
|
+
try {
|
|
927
|
+
// SVG string from file or generated QR code
|
|
928
|
+
const svgString = `
|
|
929
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">
|
|
930
|
+
<!-- SVG QR code content here -->
|
|
931
|
+
</svg>
|
|
932
|
+
`;
|
|
933
|
+
|
|
934
|
+
// Or read from file
|
|
935
|
+
// const svgString = fs.readFileSync('path/to/qr-code.svg', 'utf8');
|
|
936
|
+
|
|
937
|
+
// Validate the QR code from SVG
|
|
938
|
+
const result = await QRCodeJs.validateSvg(svgString);
|
|
939
|
+
|
|
940
|
+
if (result.isValid) {
|
|
941
|
+
console.log(`SVG QR code is valid! Decoded text: ${result.data}`);
|
|
942
|
+
console.log(`Validator used: ${result.validator}`);
|
|
943
|
+
} else {
|
|
944
|
+
console.warn(`SVG QR code validation failed: ${result.message}`);
|
|
945
|
+
}
|
|
946
|
+
} catch (error) {
|
|
947
|
+
console.error('Error validating SVG:', error);
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
validateQRFromSVG();
|
|
952
|
+
```
|
|
953
|
+
|
|
954
|
+
**Example 3: Batch Validation of Multiple QR Codes**
|
|
955
|
+
|
|
956
|
+
```typescript
|
|
957
|
+
import { QRCodeJs } from '@qr-platform/qr-code.js/node';
|
|
958
|
+
import fs from 'fs';
|
|
959
|
+
import path from 'path';
|
|
960
|
+
|
|
961
|
+
// Ensure license is activated first
|
|
962
|
+
// await QRCodeJs.license('YOUR-LICENSE-KEY');
|
|
963
|
+
|
|
964
|
+
async function batchValidateQRCodes() {
|
|
965
|
+
const qrDirectory = 'path/to/qr-codes/';
|
|
966
|
+
const results = [];
|
|
967
|
+
|
|
968
|
+
try {
|
|
969
|
+
const files = fs.readdirSync(qrDirectory);
|
|
970
|
+
|
|
971
|
+
for (const file of files) {
|
|
972
|
+
const filePath = path.join(qrDirectory, file);
|
|
973
|
+
const ext = path.extname(file).toLowerCase();
|
|
974
|
+
|
|
975
|
+
let validationResult;
|
|
976
|
+
|
|
977
|
+
if (['.png', '.jpg', '.jpeg', '.gif', '.bmp'].includes(ext)) {
|
|
978
|
+
// Validate image files
|
|
979
|
+
const imageBuffer = fs.readFileSync(filePath);
|
|
980
|
+
validationResult = await QRCodeJs.validateImageData(imageBuffer);
|
|
981
|
+
} else if (ext === '.svg') {
|
|
982
|
+
// Validate SVG files
|
|
983
|
+
const svgString = fs.readFileSync(filePath, 'utf8');
|
|
984
|
+
validationResult = await QRCodeJs.validateSvg(svgString);
|
|
985
|
+
} else {
|
|
986
|
+
console.log(`Skipping unsupported file: ${file}`);
|
|
987
|
+
continue;
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
results.push({
|
|
991
|
+
file,
|
|
992
|
+
isValid: validationResult.isValid,
|
|
993
|
+
data: validationResult.data,
|
|
994
|
+
message: validationResult.message,
|
|
995
|
+
validator: validationResult.validator
|
|
996
|
+
});
|
|
997
|
+
|
|
998
|
+
console.log(`${file}: ${validationResult.isValid ? 'VALID' : 'INVALID'} - ${validationResult.data || validationResult.message}`);
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
// Summary
|
|
1002
|
+
const validCount = results.filter(r => r.isValid).length;
|
|
1003
|
+
const totalCount = results.length;
|
|
1004
|
+
console.log(`\nBatch validation complete: ${validCount}/${totalCount} QR codes are valid`);
|
|
1005
|
+
|
|
1006
|
+
return results;
|
|
1007
|
+
} catch (error) {
|
|
1008
|
+
console.error('Error during batch validation:', error);
|
|
1009
|
+
return [];
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
batchValidateQRCodes();
|
|
1014
|
+
```
|
|
1015
|
+
|
|
1016
|
+
**Example 4: Validation with Error Handling and Retry Logic**
|
|
1017
|
+
|
|
1018
|
+
```typescript
|
|
1019
|
+
import { QRCodeJs } from '@qr-platform/qr-code.js/node';
|
|
1020
|
+
|
|
1021
|
+
// Ensure license is activated first
|
|
1022
|
+
// await QRCodeJs.license('YOUR-LICENSE-KEY');
|
|
1023
|
+
|
|
1024
|
+
async function validateWithRetry(imageData, maxRetries = 3) {
|
|
1025
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
1026
|
+
try {
|
|
1027
|
+
console.log(`Validation attempt ${attempt}/${maxRetries}`);
|
|
1028
|
+
|
|
1029
|
+
const result = await QRCodeJs.validateImageData(imageData);
|
|
1030
|
+
|
|
1031
|
+
if (result.isValid) {
|
|
1032
|
+
console.log(`✅ Validation successful on attempt ${attempt}`);
|
|
1033
|
+
return result;
|
|
1034
|
+
} else {
|
|
1035
|
+
console.warn(`❌ Validation failed on attempt ${attempt}: ${result.message}`);
|
|
1036
|
+
|
|
1037
|
+
if (attempt === maxRetries) {
|
|
1038
|
+
throw new Error(`Validation failed after ${maxRetries} attempts: ${result.message}`);
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
// Wait before retrying
|
|
1042
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
1043
|
+
}
|
|
1044
|
+
} catch (error) {
|
|
1045
|
+
console.error(`Error on attempt ${attempt}:`, error.message);
|
|
1046
|
+
|
|
1047
|
+
if (attempt === maxRetries) {
|
|
1048
|
+
throw error;
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
// Wait before retrying
|
|
1052
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
// Usage
|
|
1058
|
+
async function validateQRWithRetry() {
|
|
1059
|
+
try {
|
|
1060
|
+
const imageBuffer = fs.readFileSync('path/to/problematic-qr.png');
|
|
1061
|
+
const result = await validateWithRetry(imageBuffer);
|
|
1062
|
+
console.log('Final result:', result);
|
|
1063
|
+
} catch (error) {
|
|
1064
|
+
console.error('All validation attempts failed:', error.message);
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
validateQRWithRetry();
|
|
1069
|
+
```
|
|
1070
|
+
|
|
1071
|
+
**Example 5: Integration with Express.js API**
|
|
1072
|
+
|
|
1073
|
+
```typescript
|
|
1074
|
+
import express from 'express';
|
|
1075
|
+
import multer from 'multer';
|
|
1076
|
+
import { QRCodeJs } from '@qr-platform/qr-code.js/node';
|
|
1077
|
+
|
|
1078
|
+
const app = express();
|
|
1079
|
+
const upload = multer({ memory: true });
|
|
1080
|
+
|
|
1081
|
+
// Ensure license is activated on server startup
|
|
1082
|
+
// await QRCodeJs.license('YOUR-LICENSE-KEY');
|
|
1083
|
+
|
|
1084
|
+
// API endpoint for QR code validation
|
|
1085
|
+
app.post('/api/validate-qr', upload.single('qrImage'), async (req, res) => {
|
|
1086
|
+
try {
|
|
1087
|
+
if (!req.file) {
|
|
1088
|
+
return res.status(400).json({ error: 'No image file provided' });
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
const imageBuffer = req.file.buffer;
|
|
1092
|
+
const result = await QRCodeJs.validateImageData(imageBuffer);
|
|
1093
|
+
|
|
1094
|
+
res.json({
|
|
1095
|
+
success: true,
|
|
1096
|
+
validation: {
|
|
1097
|
+
isValid: result.isValid,
|
|
1098
|
+
data: result.data,
|
|
1099
|
+
message: result.message,
|
|
1100
|
+
validator: result.validator
|
|
1101
|
+
}
|
|
1102
|
+
});
|
|
1103
|
+
} catch (error) {
|
|
1104
|
+
console.error('Validation API error:', error);
|
|
1105
|
+
res.status(500).json({
|
|
1106
|
+
success: false,
|
|
1107
|
+
error: 'Internal server error during validation'
|
|
1108
|
+
});
|
|
1109
|
+
}
|
|
1110
|
+
});
|
|
1111
|
+
|
|
1112
|
+
// API endpoint for SVG validation
|
|
1113
|
+
app.post('/api/validate-qr-svg', express.text({ type: 'image/svg+xml' }), async (req, res) => {
|
|
1114
|
+
try {
|
|
1115
|
+
const svgString = req.body;
|
|
1116
|
+
|
|
1117
|
+
if (!svgString) {
|
|
1118
|
+
return res.status(400).json({ error: 'No SVG content provided' });
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
const result = await QRCodeJs.validateSvg(svgString);
|
|
1122
|
+
|
|
1123
|
+
res.json({
|
|
1124
|
+
success: true,
|
|
1125
|
+
validation: {
|
|
1126
|
+
isValid: result.isValid,
|
|
1127
|
+
data: result.data,
|
|
1128
|
+
message: result.message,
|
|
1129
|
+
validator: result.validator
|
|
1130
|
+
}
|
|
1131
|
+
});
|
|
1132
|
+
} catch (error) {
|
|
1133
|
+
console.error('SVG validation API error:', error);
|
|
1134
|
+
res.status(500).json({
|
|
1135
|
+
success: false,
|
|
1136
|
+
error: 'Internal server error during SVG validation'
|
|
1137
|
+
});
|
|
1138
|
+
}
|
|
1139
|
+
});
|
|
1140
|
+
|
|
1141
|
+
app.listen(3000, () => {
|
|
1142
|
+
console.log('QR validation API server running on port 3000');
|
|
1143
|
+
});
|
|
1144
|
+
```
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
---
|
|
2
|
+
title: 'API Reference Guide'
|
|
3
|
+
description: 'Complete API reference for QRCode.js library'
|
|
4
|
+
---
|
|
3
5
|
|
|
4
|
-
### Basic
|
|
6
|
+
### Basic QR Code Creation with QRCode.js
|
|
5
7
|
|
|
6
8
|
```typescript
|
|
7
9
|
import { QRCodeJs, Options } from '@qr-platform/qr-code.js';
|
|
@@ -122,7 +124,17 @@ qrCode.append(document.getElementById('qr-container'));
|
|
|
122
124
|
| `token` | `token: string | null` | Activates a license using a pre-fetched token. Returns `Promise<ValidationResult>`. |
|
|
123
125
|
| `configureLicenseFetcher` | `fetcher: (licenseKey: string) => Promise<string>` | Sets a custom function for fetching license tokens. |
|
|
124
126
|
| `setLicenseUrl` | `url: string` | Sets the URL endpoint for license validation. Returns `typeof QRCodeJs`. |
|
|
125
|
-
| `validateImageData` | `imageData: ImageDataLike` | **(Node.js)** Validate scannability from raw image data. Returns `Promise<ScanValidatorResponse>`. |
|
|
127
|
+
| `validateImageData` | `imageData: ImageDataLike` | **(Node.js Static)** Validate scannability from raw image data. Returns `Promise<ScanValidatorResponse>`. |
|
|
128
|
+
| `validateSvg` | `svgSource: string` | **(Node.js Static)** Validate scannability from SVG string. Returns `Promise<ScanValidatorResponse>`. |
|
|
129
|
+
| `setId` | `id: string` | Sets an identifier for the QR code instance. Returns `this`. |
|
|
130
|
+
| `getId` | - | Gets the identifier for the QR code instance. Returns `string \| undefined`. |
|
|
131
|
+
| `setName` | `name: string` | Sets a name for the QR code instance. Returns `this`. |
|
|
132
|
+
| `getName` | - | Gets the name for the QR code instance. Returns `string \| undefined`. |
|
|
133
|
+
| `setDescription` | `description: string` | Sets a description for the QR code instance. Returns `this`. |
|
|
134
|
+
| `getDescription` | - | Gets the description for the QR code instance. Returns `string \| undefined`. |
|
|
135
|
+
| `setMetadata` | `metadata: Record<string, any>` | Sets custom metadata for the QR code instance. Returns `this`. |
|
|
136
|
+
| `getMetadata` | - | Gets the custom metadata for the QR code instance. Returns `Record<string, any> \| undefined`. |
|
|
137
|
+
| `getSettings` | - | Gets the current settings and options for the QR code instance. Returns `SettingsOptions \| undefined`. |
|
|
126
138
|
|
|
127
139
|
---
|
|
128
140
|
|
|
@@ -322,13 +334,9 @@ const qr3 = QRCodeJs.useTemplate('basic')
|
|
|
322
334
|
| `useData` | `data: string, overrideOpts?: MethodOverrideOptions` | Applies a data string to the current builder configuration. If `overrideOpts.override` is `true`, this data will take precedence over data provided in the final `.options()` call. Returns `this`. |
|
|
323
335
|
| `useOptions` | `options: RecursivePartial<Options>, overrideOpts?: MethodOverrideOptions` | Applies a partial options object to the current builder configuration. If `overrideOpts.override` is `true`, these options take higher precedence over options provided in the final `.options()` call for the properties they cover. Returns `this`. |
|
|
324
336
|
| `useSettings` | `settings: SettingsOptions` | Applies a comprehensive `SettingsOptions` object as a new baseline for the builder chain. This will **reset** any configurations previously applied to *that builder instance* via other `use` methods. Subsequent builder methods modify this new baseline. Returns `this`. |
|
|
337
|
+
| `useId` | `id: string` | Assigns an identifier to the QR code instance being built. Returns `this`. |
|
|
338
|
+
| `useName` | `name: string` | Assigns a name to the QR code instance being built. Returns `this`. |
|
|
339
|
+
| `useDescription` | `description: string` | Assigns a description to the QR code instance being built. Returns `this`. |
|
|
340
|
+
| `useMetadata` | `metadata: Record<string, any>` | Attaches custom metadata to the QR code instance being built. Returns `this`. |
|
|
325
341
|
| `options` | `options: RecursivePartial<Options>` | Merges the provided `Options` into the current configuration and creates and returns the final `QRCodeJs` instance. |
|
|
326
342
|
| `build` | - | Creates and returns the final `QRCodeJs` instance based on the accumulated configuration. |
|
|
327
|
-
### See Also
|
|
328
|
-
- [QRCode.js Documentation](./documentation.md#start)
|
|
329
|
-
- [Quick References Guide](./quick-references-guide.md#start)
|
|
330
|
-
- [API Reference Guide](./api-reference-guide.md#start)
|
|
331
|
-
- [TypeScript Types and Definitions](./typescript-types-definitions.md#start)
|
|
332
|
-
- [License Management](./license-management.md#start)
|
|
333
|
-
- [Basic Examples](./examples.md#start)
|
|
334
|
-
- [Advanced Examples](./advanced-examples.md#start)
|