(api: Api, options?: {
readonly baseUrl?: URL | string | undefined
})
| 658 | * @since 4.0.0 |
| 659 | */ |
| 660 | export const urlBuilder = <Api extends HttpApi.Constraint>(api: Api, options?: { |
| 661 | readonly baseUrl?: URL | string | undefined |
| 662 | }): UrlBuilder<Api> => { |
| 663 | const builder: Record<string, any> = {} |
| 664 | |
| 665 | HttpApi.reflect(api as unknown as HttpApi.Top, { |
| 666 | onGroup({ group }) { |
| 667 | if (group.topLevel) return |
| 668 | InternalRecord.assignProperty(builder, group.identifier, {}) |
| 669 | }, |
| 670 | onEndpoint({ group, endpoint }) { |
| 671 | const makeUrl = compilePath(endpoint.path) |
| 672 | const encodeParams = endpoint.params === undefined |
| 673 | ? undefined |
| 674 | : Schema.encodeSync(endpoint.params as unknown as Schema.ConstraintEncoder<unknown>) |
| 675 | const encodeQuery = endpoint.query === undefined |
| 676 | ? undefined |
| 677 | : Schema.encodeSync(endpoint.query as unknown as Schema.ConstraintEncoder<unknown>) |
| 678 | |
| 679 | const endpointBuilder = (request?: { |
| 680 | readonly params?: unknown |
| 681 | readonly query?: unknown |
| 682 | }) => { |
| 683 | const params = request?.params |
| 684 | const path = params === undefined |
| 685 | ? endpoint.path |
| 686 | : makeUrl((encodeParams === undefined ? params : encodeParams(params)) as Record<string, string | undefined>) |
| 687 | const queryInput = request?.query === undefined |
| 688 | ? undefined |
| 689 | : (encodeQuery === undefined ? request.query : encodeQuery(request.query)) as UrlParams.Input |
| 690 | const query = queryInput === undefined ? "" : UrlParams.toString(UrlParams.fromInput(queryInput)) |
| 691 | const url = query === "" ? path : `${path}?${query}` |
| 692 | return options?.baseUrl === undefined ? url : new URL(url, options.baseUrl.toString()).toString() |
| 693 | } |
| 694 | InternalRecord.assignProperty( |
| 695 | group.topLevel ? builder : builder[group.identifier], |
| 696 | endpoint.identifier, |
| 697 | endpointBuilder |
| 698 | ) |
| 699 | } |
| 700 | }) |
| 701 | |
| 702 | return builder as UrlBuilder<Api> |
| 703 | } |
| 704 | |
| 705 | // ---------------------------------------------------------------------------- |
| 706 |
nothing calls this directly
no outgoing calls
no test coverage detected