146 lines
5.4 KiB
Dart
146 lines
5.4 KiB
Dart
import 'package:teamhydra_idgen/src/idgen_http.dart';
|
|
import 'package:teamhydra_idgen/teamhydra_idgen.dart';
|
|
|
|
/// The main class to use when generating an ID
|
|
///
|
|
/// All returned IDs are in the form of a [IDGenResponse] object which contains the ID.
|
|
/// If an error occurs, an [IDGenException] is thrown.
|
|
class IDGen {
|
|
final String username;
|
|
final String token;
|
|
final IDGenHTTPWorker _worker;
|
|
|
|
/// Create a new IDGen class to generate IDs
|
|
///
|
|
/// [username] and [token] are required to authenticate with the IDGen API.
|
|
IDGen({required this.username, required this.token})
|
|
: _worker = IDGenHTTPWorker(username: username, token: token);
|
|
|
|
/// Generate a new UUID V4
|
|
///
|
|
/// Returns a [IDGenResponse] object containing the generated ID.
|
|
/// Throws an [IDGenException] if an error occurs.
|
|
Future<IDGenResponse> generateUUIDV4() async {
|
|
return await _worker.generate('uuid', null);
|
|
}
|
|
|
|
/// Generate a new nanoID
|
|
///
|
|
/// Returns a [IDGenResponse] object containing the generated ID.
|
|
/// Optionally, you can pass a [size] parameter to specify the length of the nanoID, default is 10.
|
|
/// You can also pass a [alphabet] parameter to specify the characters used in the nanoID, default is provided by the API.
|
|
///
|
|
/// Throws an [IDGenException] if an error occurs.
|
|
///
|
|
/// Throws an [ArgumentError] if the size is not between 1 and 256 or the alphabet is empty.
|
|
Future<IDGenResponse> generateNanoID({int? size, String? alphabet}) async {
|
|
// Ensure length is between 1 and 256 (if specified)
|
|
if (size != null && (size < 1 || size > 256)) {
|
|
throw ArgumentError(
|
|
'Cannot generate a nanoID with a length of $size, must be between 1 and 256');
|
|
}
|
|
|
|
// Ensure alphabet is not empty (if specified)
|
|
if (alphabet != null && alphabet.isEmpty) {
|
|
throw ArgumentError('Cannot generate a nanoID with an empty alphabet');
|
|
}
|
|
|
|
// Ensure alphabet is at least 3 characters long and not longer than 256
|
|
if (alphabet != null && (alphabet.length < 3 || alphabet.length > 256)) {
|
|
throw ArgumentError(
|
|
'Cannot generate a nanoID with an alphabet of length ${alphabet.length}, must be between 3 and 256');
|
|
}
|
|
|
|
return await _worker.generate('nanoid', {
|
|
if (size != null) 'length': size else 'length': 10,
|
|
if (alphabet != null) 'alphabet': alphabet,
|
|
});
|
|
}
|
|
|
|
/// Generate a 2FA code pair
|
|
///
|
|
/// Returns a [IDGenResponse] object containing the generated ID.
|
|
///
|
|
/// Optionally, you can pass a [length] parameter to specify the length of the 2FA code, default is 6.
|
|
///
|
|
/// Throws an [IDGenException] if an error occurs.
|
|
///
|
|
/// Throws an [ArgumentError] if the length is not between 1 and 256.
|
|
Future<IDGenResponse> generate2FACode({int? length}) async {
|
|
// Ensure length is between 1 and 256 (if specified)
|
|
if (length != null && (length < 1 || length > 256)) {
|
|
throw ArgumentError(
|
|
'Cannot generate a 2FA code with a length of $length, must be between 1 and 256');
|
|
}
|
|
|
|
return await _worker.generate('2fa', {
|
|
if (length != null) 'length': length,
|
|
});
|
|
}
|
|
|
|
/// Generate a license key
|
|
///
|
|
/// Returns a [IDGenResponse] object containing the generated ID.
|
|
/// Keys are generated with a 25 character length, resulting in a 5-5-5-5-5 format.
|
|
///
|
|
/// Throws an [IDGenException] if an error occurs.
|
|
Future<IDGenResponse> generateLicenseKey() async {
|
|
return await _worker.generate('license', null);
|
|
}
|
|
|
|
/// Generate word based string
|
|
///
|
|
/// Returns a [IDGenResponse] object containing the generated ID.
|
|
/// Optionally, you can pass a [length] parameter to specify the length of the word based string, default is 5.
|
|
/// You can also pass a [separator] parameter to specify the separator used in the word based string, options are 'slug', 'title' and 'formal'. Default is 'slug'.
|
|
///
|
|
/// Slug: lowercase words separated by hyphens
|
|
///
|
|
/// Title: Title case words separated by spaces
|
|
///
|
|
/// Formal: Title case words with no spaces or separators
|
|
///
|
|
/// Throws an [IDGenException] if an error occurs.
|
|
///
|
|
/// Throws an [ArgumentError] if the length is not between 1 and 16 or the separator is invalid.
|
|
Future<IDGenResponse> generateWordBasedString(
|
|
{int? length, String? separator}) async {
|
|
// Ensure length is between 1 and 256 (if specified)
|
|
if (length != null && (length < 1 || length > 16)) {
|
|
throw ArgumentError(
|
|
'Cannot generate a word based string with a length of $length, must be between 1 and 16');
|
|
}
|
|
|
|
// Ensure separator is valid (if specified)
|
|
if (separator != null &&
|
|
separator != 'slug' &&
|
|
separator != 'title' &&
|
|
separator != 'formal') {
|
|
throw ArgumentError(
|
|
'Cannot generate a word based string with an invalid separator');
|
|
}
|
|
|
|
return await _worker.generate('word', {
|
|
if (length != null) 'length': length else 'length': 5,
|
|
if (separator != null) 'style': separator else 'style': 'slug',
|
|
});
|
|
}
|
|
|
|
/// Generate a snowflake ID
|
|
///
|
|
/// Returns a [IDKeypairResponse] object containing
|
|
///
|
|
/// Throws an [IDGenException] if an error occurs.
|
|
Future<IDGenResponse> generateSnowflakeID() async {
|
|
return await _worker.generate('snowflake', null);
|
|
}
|
|
|
|
/// Generate a keypair
|
|
///
|
|
/// Returns a [IDKeypairResponse] object containing the generated ID and secret key.
|
|
/// Throws an [IDGenException] if an error occurs.
|
|
Future<IDKeypairResponse> generateKeypair() async {
|
|
return await _worker.generateKeypair();
|
|
}
|
|
}
|