register<T extends IRequestInterface, E> method

void register<T extends IRequestInterface, E>({
  1. required T interface,
  2. required JsonEncoderCallback encoder,
  3. required JsonDecoderCallback<E> decoder,
  4. String? key,
  5. bool overriteIfTypeExists = false,
})

Called to initialize an interface. All interfaces inherit from IRequestInterface abstract class, which provides internal requester instance and other functions.

key must be unique to this instance of WordpressClient as this will be used to indentify the instance & the response type used by the interface requests.

interface is instance of interface type T

decoder is a function that takes a json object and returns an instance of T

encoder is a function that takes an instance of T and returns a json object These are required to decode and encode responses for this interface.

overriteIfTypeExists is a boolean that determines if the type should be overwritten if it already exists.

Some keys are already occupied:

  • me
  • posts
  • categories
  • comments
  • media
  • tags
  • users
  • search
  • pages
  • application-passwords

Example usage:

await initInterface<UsersInterface, User>(
  interface: CustomInterface(),
  key: 'custom_interface_key',
  responseDecoder: [CustomResponseObject].fromJson,
  responseEncoder: (dynamic response) => (response as [CustomResponseObject]).toJson(),
);

Implementation

void register<T extends IRequestInterface, E>({
  required T interface,
  required JsonEncoderCallback encoder,
  required JsonDecoderCallback<E> decoder,
  String? key,
  bool overriteIfTypeExists = false,
}) {
  final interfaceKey = InterfaceKey<T>(key);

  if (_interfaces[interfaceKey] != null) {
    throw InterfaceExistException<T>();
  }

  _registerResponseType<E>(
    decoder: decoder,
    encoder: encoder,
    overriteIfExists: overriteIfTypeExists,
  );

  interface._initInterface(
    requester: _requester,
    key: interfaceKey,
  );

  _interfaces[interfaceKey] = interface;
}