Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(typescript): Improve adapter typings #2605

Merged
merged 1 commit into from
Apr 23, 2022
Merged

feat(typescript): Improve adapter typings #2605

merged 1 commit into from
Apr 23, 2022

Conversation

daffl
Copy link
Member

@daffl daffl commented Apr 23, 2022

This is a follow-up to #2600 and closes #2564.

It updates the adapter types with the proper overloads, most importantly returning an array when passing { paginate: false }. It also adds an AdapterBase service from which other adapters can now extend implementing only the hook-less service methods and no service interface. The reason is that generated services will now extend from those and then implement the official service methods which will by default in turn call their internal counterpart. This allows to implement the service methods you want with different parameter types and return values without being restricted by any incompatible interfaces.

export class UserService extends MemoryAdapter<UserResult, UserData, UserServiceParams> {
  async find (params?: UserServiceParams & { paginate?: PaginationOptions }): Promise<Paginated<T>>;
  async find (params?: UserServiceParams & { paginate: false }): Promise<UserResult[]>;
  async find (params?: UserServiceParams): Promise<Paginated<UserResult>|UserResult[]>;
  async find (params?: UserServiceParams): Promise<Paginated<UserResult>|UserResult[]> {
    return this._find(params);
  }

  async get (id: Id, params?: UserServiceParams) {
    return {
      id,
      message: 'I can return whatever I want here'
    };
  }

  async create (data: UserCreateData, params?: UserServiceParams) {
    return this._create(data, params);
  }

  async update (id: Id, data: D, params?: UserServiceParams): Promise<T> {
    return this._update(id, data, params);
  }

  async patch (id: Id, data: Partial<D>, params?: UserServiceParams): Promise<UserResult>;
  async patch (id: null, data: Partial<D>, params?: UserServiceParams): Promise<UserResult[]>;
  async patch (id: NullableId, data: Partial<D>, params?: UserServiceParams): Promise<UserResult | UserResult[]> {
    return this._patch(id, data, params);
  }

  async remove (id: Id, params?: UserServiceParams): Promise<UserResult>;
  async remove (id: null, params?: UserServiceParams): Promise<UserResult[]>;
  async remove (id: NullableId, params?: UserServiceParams): Promise<UserResult | UserResult[]> {
    return this._remove(id, params);
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pagination and query TypeScript improvements
1 participant