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

What is the supposed workflow when using parameter types for API calls? #55

Open
jwbth opened this issue Jan 4, 2025 · 2 comments
Open

Comments

@jwbth
Copy link

jwbth commented Jan 4, 2025

Let's say I want to make sure the parameters I use for my calls to a certain API comply with the contract. What is my workflow for that?

E.g. when I do

const options = {
  'action': 'compare',
  'totitle': 'Text',
  'toslots': 'main',
  'totext-main': 'test',
  'topst': true,
  'prop': 'diff',
  'fromslots': 'main',
  'fromtext-main': '',
} as import('types-mediawiki/api_params').ApiComparePagesParams;
new mw.Api().post(options);

options gets highlighted by TypeScript with the error

Argument of type 'ApiComparePagesParams' is not assignable to parameter of type 'UnknownApiParams'.
  Index signature for type 'string' is missing in type 'ApiComparePagesParams'.ts(2345)

Indeed, there is no index signature in the ApiComparePagesParams interface.

UnknownApiParams is

type UnknownApiParams = Record<string, string | number | boolean | string[] | number[] | undefined>;

@jwbth jwbth changed the title What is the supposed workflow when using API parameter types? What is the supposed workflow when using parameter types for API calls? Jan 4, 2025
@Derugon
Copy link
Collaborator

Derugon commented Jan 7, 2025

Well, ApiParams (or any extending interface from api_params) can not be used to narrow UnknownApiParams, which I assume is a bug.

A fix proposed in #41 is to make ApiParams extend UnknownApiParams, and allow File objects in UnknownApiParams to prevent breakage, i.e.:

interface ApiParams extends UnknownApiParams { ... }
type UnknownApiParams = Record<string, string | number | boolean | File | string[] | number[] | undefined>;

If that's ok, I can apply that fix to the base branch.

@jwbth
Copy link
Author

jwbth commented Jan 8, 2025

If that's ok, I can apply that fix to the base branch.

Sounds good.

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

No branches or pull requests

2 participants