endpoints / Function

createEndpointsInjectionToken

Generic types:Schemas

Convenience function that creates a typed InjectionToken with a default value of a collection of endpoints generated from the given schemas, through generateEndpoints.

See Also

Presentation

function createEndpointsInjectionToken(
  name: string,
  schemas: Schemas,
): InjectionToken<GenerateEndpoints<Schemas>>;

Returns

Parameters

NameTypeDescription
name
string
schemas
Schemas

Example usage

const USER_ENDPOINTS = new InjectionToken("USER_ENDPOINTS", {
  factory: createEndpointsFactory({
    list: {
      path: '/api/users',
      method: 'GET',
      params: null,
      response: $type<User[]>(),
    },
    create: {
      path: '/api/users',
      method: 'POST',
      params: { name: $type<string>(), 'gender?': $type<string>() },
      response: $type<User>(),
    },
  }),
});
private userEndpoints = inject(USER_ENDPOINTS);
this.userEndpoints.list().subscribe(users => console.log(users));
this.userEndpoints.create({ name: 'Char2s' }).subscribe(user => console.log(user));
this.userEndpoints.create({ name: 'Char2s', gender: "Male" }).subscribe(user => console.log(user));