createRequest function

Code createRequest(
  1. ClientMethod method
)

Implementation

Code createRequest(ClientMethod method) {
  final headers = <String, Code>{};

  final (
    cookies: cookieParams,
    body: bodyParams,
    query: queryParams,
    headers: headerParams,
  ) = method.separateParams;

  if (cookieParams.isNotEmpty) {
    headers['cookie'] = createCookieHeader(cookieParams);
  }

  if (headerParams.isNotEmpty) {
    for (final param in headerParams) {
      final key = switch (param.access) {
        [final String access] => access,
        _ => param.name,
      };
      headers[key] = refer(param.name).code;
    }
  }

  final call = refer('_client').property('request').call([], {
    'method': refer("'${method.method}'"),
    'path': refer("'${method.resolvedPath}'"),
    if (headers.isNotEmpty) 'headers': createMap(headers),
    if (queryParams.isNotEmpty) 'query': createQueryArg(queryParams),
    if (bodyParams.isNotEmpty) 'body': createBodyArg(bodyParams),
  }).awaited;

  if (method.returnType.isVoid) {
    return call.statement;
  }

  return declareFinal('response').assign(call).statement;
}