nyApi<T> function

Future nyApi<T>({
  1. required dynamic request(
    1. T
    ),
  2. Map<Type, dynamic> apiDecoders = const {},
  3. BuildContext? context,
  4. Map<String, dynamic> headers = const {},
  5. String? bearerToken,
  6. String? baseUrl,
})

API helper

Implementation

Future<dynamic> nyApi<T>(
    {required dynamic Function(T) request,
    Map<Type, dynamic> apiDecoders = const {},
    BuildContext? context,
    Map<String, dynamic> headers = const {},
    String? bearerToken,
    String? baseUrl}) async {
  assert(apiDecoders.containsKey(T),
      'Your config/decoders.dart is missing this class ${T.toString()} in apiDecoders.');

  dynamic apiService = apiDecoders[T];

  if (context != null) {
    apiService.setContext(context);
  }

  // add headers
  if (headers.isNotEmpty) {
    apiService.setHeaders(headers);
  }

  // add bearer token
  if (bearerToken != null) {
    apiService.setBearerToken(bearerToken);
  }

  // add baseUrl
  if (baseUrl != null) {
    apiService.setBaseUrl(baseUrl);
  }

  return await request(apiService);
}