Simplifying Type Handling for API Responses in Effect Typescript Library

Hi,
I've created an Http Client with @effect/platform
and I've created some utility functions to make the api simpler and I'm consuming it like so:
export const getAllPermissionsGroups = async <
  View extends CollectionViewParamType['view'],
>({
  view,
}: CollectionViewParamType) => {
  if (view === 'select') {
    const permissionsGroupsQuery = apiQuery({
      url: 'v2/permission-groups',
      queryParsers: { view: parseAsString.withDefault('select') },
    });

    return await Effect.runPromise(
      permissionsGroupsQuery({ view }).pipe(
        Effect.flatMap(
          validateResponse(Schema.Array(PermissionGroupSelectSchema)),
        ),
        Effect.provide(ApiHttpClientLayer),
      ),
    );
  }

  // Handle 'list' view
  const permissionsGroupsQuery = apiQuery({
    url: 'v2/permission-groups',
    queryParsers: { view: parseAsString.withDefault('list') },
  });

  return await Effect.runPromise(
    permissionsGroupsQuery({ view })
      .pipe(
        Effect.flatMap(validateResponse(Schema.Array(PermissionsGroupListSchema))),
        Effect.provide(ApiHttpClientLayer),
      ),
  );
};

However, this is bugging me because, first I need to declare two function that do the same thing, just the response validation is different, second, the return type of getAllPermissionsGroups is Promise<PermissionGroupSelectSchema[]> | Promise<PermissionsGroupListSchema[]> And this is not very helpful.

Before I was using library ky and I could do this:
export async function getAllPermissions<
  View extends CollectionViewParamType['view'],
>({ view }: CollectionViewParamType) {
    const apiClient = await getApiClient();
    return await apiClient
      .get<PermissionResultType<View>>('v2/permissions', {
        searchParams: {
          view,
        },
      })
      .json();
}

I was able to use a type helper to narrow down the return type based on view

Is there a way to achieve the same thing with my new approach as with ky ?
Was this page helpful?