ox
Version:
Ethereum Standard Library
262 lines • 7.86 kB
TypeScript
import * as Bytes from './Bytes.js';
import * as Errors from './Errors.js';
import * as Hex from './Hex.js';
/**
* Encodes a {@link ox#Bytes.Bytes} to a Base64-encoded string (with optional padding and/or URL-safe characters).
*
* @example
* ```ts twoslash
* import { Base64, Bytes } from 'ox'
*
* const value = Base64.fromBytes(
* Bytes.fromString('hello world')
* )
* // @log: 'aGVsbG8gd29ybGQ='
* ```
*
* @example
* ### No Padding
*
* Turn off [padding of encoded data](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) with the `pad` option:
*
* ```ts twoslash
* import { Base64, Bytes } from 'ox'
*
* const value = Base64.fromBytes(
* Bytes.fromString('hello world'),
* { pad: false }
* )
* // @log: 'aGVsbG8gd29ybGQ'
* ```
*
* ### URL-safe Encoding
*
* Turn on [URL-safe encoding](https://datatracker.ietf.org/doc/html/rfc4648#section-5) (Base64 URL) with the `url` option:
*
* ```ts twoslash
* import { Base64, Bytes } from 'ox'
*
* const value = Base64.fromBytes(
* Bytes.fromString('hello wod'),
* { url: true }
* )
* // @log: 'aGVsbG8gd29_77-9ZA=='
* ```
*
* @param value - The byte array to encode.
* @param options - Encoding options.
* @returns The Base64 encoded string.
*/
export declare function fromBytes(value: Bytes.Bytes, options?: fromBytes.Options): string;
export declare namespace fromBytes {
type Options = {
/**
* Whether to [pad](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) the Base64 encoded string.
*
* @default true
*/
pad?: boolean | undefined;
/**
* Whether to Base64 encode with [URL safe characters](https://datatracker.ietf.org/doc/html/rfc4648#section-5).
*
* @default false
*/
url?: boolean | undefined;
};
type ErrorType = Errors.GlobalErrorType;
}
/**
* Encodes a {@link ox#Hex.Hex} to a Base64-encoded string (with optional padding and/or URL-safe characters).
*
* @example
* ```ts twoslash
* import { Base64, Hex } from 'ox'
*
* const value = Base64.fromHex(Hex.fromString('hello world'))
* // @log: 'aGVsbG8gd29ybGQ='
* ```
*
* @example
* ### No Padding
*
* Turn off [padding of encoded data](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) with the `pad` option:
*
* ```ts twoslash
* import { Base64, Hex } from 'ox'
*
* const value = Base64.fromHex(
* Hex.fromString('hello world'),
* { pad: false }
* )
* // @log: 'aGVsbG8gd29ybGQ'
* ```
*
* ### URL-safe Encoding
*
* Turn on [URL-safe encoding](https://datatracker.ietf.org/doc/html/rfc4648#section-5) (Base64 URL) with the `url` option:
*
* ```ts twoslash
* import { Base64, Hex } from 'ox'
*
* const value = Base64.fromHex(Hex.fromString('hello wod'), {
* url: true
* })
* // @log: 'aGVsbG8gd29_77-9ZA=='
* ```
*
* @param value - The hex value to encode.
* @param options - Encoding options.
* @returns The Base64 encoded string.
*/
export declare function fromHex(value: Hex.Hex, options?: fromHex.Options): string;
export declare namespace fromHex {
type Options = {
/**
* Whether to [pad](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) the Base64 encoded string.
*
* @default true
*/
pad?: boolean | undefined;
/**
* Whether to Base64 encode with [URL safe characters](https://datatracker.ietf.org/doc/html/rfc4648#section-5).
*
* @default false
*/
url?: boolean | undefined;
};
type ErrorType = fromBytes.ErrorType | Errors.GlobalErrorType;
}
/**
* Encodes a string to a Base64-encoded string (with optional padding and/or URL-safe characters).
*
* @example
* ```ts twoslash
* import { Base64 } from 'ox'
*
* const value = Base64.fromString('hello world')
* // @log: 'aGVsbG8gd29ybGQ='
* ```
*
* @example
* ### No Padding
*
* Turn off [padding of encoded data](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) with the `pad` option:
*
* ```ts twoslash
* import { Base64 } from 'ox'
*
* const value = Base64.fromString('hello world', {
* pad: false
* })
* // @log: 'aGVsbG8gd29ybGQ'
* ```
*
* ### URL-safe Encoding
*
* Turn on [URL-safe encoding](https://datatracker.ietf.org/doc/html/rfc4648#section-5) (Base64 URL) with the `url` option:
*
* ```ts twoslash
* import { Base64 } from 'ox'
*
* const value = Base64.fromString('hello wod', { url: true })
* // @log: 'aGVsbG8gd29_77-9ZA=='
* ```
*
* @param value - The string to encode.
* @param options - Encoding options.
* @returns The Base64 encoded string.
*/
export declare function fromString(value: string, options?: fromString.Options): string;
export declare namespace fromString {
type Options = {
/**
* Whether to [pad](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) the Base64 encoded string.
*
* @default true
*/
pad?: boolean | undefined;
/**
* Whether to Base64 encode with [URL safe characters](https://datatracker.ietf.org/doc/html/rfc4648#section-5).
*
* @default false
*/
url?: boolean | undefined;
};
type ErrorType = fromBytes.ErrorType | Errors.GlobalErrorType;
}
/**
* Decodes a Base64-encoded string (with optional padding and/or URL-safe characters) to {@link ox#Bytes.Bytes}.
*
* @example
* ```ts twoslash
* import { Base64, Bytes } from 'ox'
*
* const value = Base64.toBytes('aGVsbG8gd29ybGQ=')
* // @log: Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100])
* ```
*
* @param value - The string, hex value, or byte array to encode.
* @returns The Base64 decoded {@link ox#Bytes.Bytes}.
*/
export declare function toBytes(value: string): Bytes.Bytes;
export declare namespace toBytes {
type ErrorType = InvalidCharacterError | InvalidLengthError | InvalidPaddingError | Errors.GlobalErrorType;
}
/**
* Decodes a Base64-encoded string (with optional padding and/or URL-safe characters) to {@link ox#Hex.Hex}.
*
* @example
* ```ts twoslash
* import { Base64, Hex } from 'ox'
*
* const value = Base64.toHex('aGVsbG8gd29ybGQ=')
* // @log: 0x68656c6c6f20776f726c64
* ```
*
* @param value - The string, hex value, or byte array to encode.
* @returns The Base64 decoded {@link ox#Hex.Hex}.
*/
export declare function toHex(value: string): Hex.Hex;
export declare namespace toHex {
type ErrorType = toBytes.ErrorType | Errors.GlobalErrorType;
}
/**
* Decodes a Base64-encoded string (with optional padding and/or URL-safe characters) to a string.
*
* @example
* ```ts twoslash
* import { Base64 } from 'ox'
*
* const value = Base64.toString('aGVsbG8gd29ybGQ=')
* // @log: 'hello world'
* ```
*
* @param value - The string, hex value, or byte array to encode.
* @returns The Base64 decoded string.
*/
export declare function toString(value: string): string;
export declare namespace toString {
type ErrorType = toBytes.ErrorType | Errors.GlobalErrorType;
}
/** Thrown when a Base64 string contains an invalid character. */
export declare class InvalidCharacterError extends Errors.BaseError {
readonly name = "Base64.InvalidCharacterError";
constructor({ character }: {
character: string;
});
}
/** Thrown when a Base64 string has an impossible length. */
export declare class InvalidLengthError extends Errors.BaseError {
readonly name = "Base64.InvalidLengthError";
constructor({ length }: {
length: number;
});
}
/** Thrown when a Base64 string contains too many trailing `=` padding characters. */
export declare class InvalidPaddingError extends Errors.BaseError {
readonly name = "Base64.InvalidPaddingError";
constructor({ padding }: {
padding: number;
});
}
//# sourceMappingURL=Base64.d.ts.map