endpoints / Function

generateEndpoint

Generic types:Schema

Resolves an EndpointSchema object into a function, which accepts an object of parameters as declared, if any, and returns an observable response.

See Also

Presentation

function generateEndpoint(
  invoker: EndpointInvoker,
  schema: Schema,
): GenerateEndpoint<Schema>;

Returns

Parameters

NameTypeDescription
invoker
EndpointInvoker

the EndpointInvoker instance to use under the hood

schema
Schema

the declaration schema of the endpoint

Example usage

const invoker = inject(EndpointInvoker);
const endpoint = generateEndpoint(invoker, {
  path: '/api/users',
  method: 'GET',
  params: null,
  response: $type<{ id: 'number', name: 'string' }>(),
})
endpoint().subscribe(response => {
  console.log(response.id);
  console.log(response.name);
})
const invoker = inject(EndpointInvoker);
const endpoint = generateEndpoint(invoker, {
  path: '/api/users',
  method: 'POST',
  params: { name: $type<string>(), 'gender?': $type<string>() },
  response: $type<{ id: 'number', name: 'string' }>(),
})
endpoint({ name: "Char2s" }).subscribe(response => {
  console.log(response.id);
  console.log(response.name);
})