index.d.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * Generate secure URL-friendly unique ID. The non-blocking version.
  3. *
  4. * By default, the ID will have 21 symbols to have a collision probability
  5. * similar to UUID v4.
  6. *
  7. * ```js
  8. * import { nanoid } from 'nanoid/async'
  9. * nanoid().then(id => {
  10. * model.id = id
  11. * })
  12. * ```
  13. *
  14. * @param size Size of the ID. The default size is 21.
  15. * @returns A promise with a random string.
  16. */
  17. export function nanoid(size?: number): Promise<string>
  18. /**
  19. * A low-level function.
  20. * Generate secure unique ID with custom alphabet. The non-blocking version.
  21. *
  22. * Alphabet must contain 256 symbols or less. Otherwise, the generator
  23. * will not be secure.
  24. *
  25. * @param alphabet Alphabet used to generate the ID.
  26. * @param defaultSize Size of the ID. The default size is 21.
  27. * @returns A function that returns a promise with a random string.
  28. *
  29. * ```js
  30. * import { customAlphabet } from 'nanoid/async'
  31. * const nanoid = customAlphabet('0123456789абвгдеё', 5)
  32. * nanoid().then(id => {
  33. * model.id = id //=> "8ё56а"
  34. * })
  35. * ```
  36. */
  37. export function customAlphabet(
  38. alphabet: string,
  39. defaultSize?: number
  40. ): (size?: number) => Promise<string>
  41. /**
  42. * Generate an array of random bytes collected from hardware noise.
  43. *
  44. * ```js
  45. * import { random } from 'nanoid/async'
  46. * random(5).then(bytes => {
  47. * bytes //=> [10, 67, 212, 67, 89]
  48. * })
  49. * ```
  50. *
  51. * @param bytes Size of the array.
  52. * @returns A promise with a random bytes array.
  53. */
  54. export function random(bytes: number): Promise<Uint8Array>