/ src / lib / utils / wave / orgs.ts
orgs.ts
 1  import { authenticatedCall } from './call';
 2  import { toFilterParams } from './types/filter';
 3  import {
 4    orgFiltersSchema,
 5    orgMemberDtoSchema,
 6    orgRepoDtoSchema,
 7    publicOrgDtoSchema,
 8    publicOrgsFiltersSchema,
 9    userOrgDtoSchema,
10    type OrgFilters,
11    type PublicOrgsFilters,
12  } from './types/org';
13  import {
14    paginatedResponseSchema,
15    toPaginationParams,
16    type PaginationInput,
17  } from './types/pagination';
18  import { getAllPaginated } from './getAllPaginated';
19  import parseRes from './utils/parse-res';
20  
21  export async function getOrgs(f = fetch, pagination?: PaginationInput, filters?: OrgFilters) {
22    const res = await authenticatedCall(
23      f,
24      `/api/orgs?${toPaginationParams(pagination)}&${toFilterParams(orgFiltersSchema, filters)}`,
25    );
26  
27    return parseRes(paginatedResponseSchema(userOrgDtoSchema), res);
28  }
29  
30  export async function getOwnRepos(f = fetch, pagination?: PaginationInput) {
31    const res = await authenticatedCall(f, `/api/repos?${toPaginationParams(pagination)}`);
32  
33    return parseRes(paginatedResponseSchema(orgRepoDtoSchema), res);
34  }
35  
36  export async function getPublicOrg(f = fetch, orgId: string) {
37    return parseRes(publicOrgDtoSchema, await authenticatedCall(f, `/api/orgs/public/${orgId}`), {
38      expect404: true,
39    });
40  }
41  
42  export async function getOrgMembers(f = fetch, orgId: string) {
43    return getAllPaginated(async (page, limit) =>
44      parseRes(
45        paginatedResponseSchema(orgMemberDtoSchema),
46        await authenticatedCall(
47          f,
48          `/api/orgs/${orgId}/public/members?${toPaginationParams({ page, limit })}`,
49        ),
50      ),
51    );
52  }
53  
54  export async function getPublicOrgs(
55    f = fetch,
56    pagination: PaginationInput = {},
57    filters: PublicOrgsFilters = {},
58  ) {
59    return parseRes(
60      paginatedResponseSchema(publicOrgDtoSchema),
61      await authenticatedCall(
62        f,
63        `/api/orgs/public?${toPaginationParams(pagination)}&${toFilterParams(publicOrgsFiltersSchema, filters)}`,
64      ),
65    );
66  }