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

Type error when passing Astro object to APIRoute #8514

Closed
1 task
minht11 opened this issue Sep 11, 2023 · 12 comments
Closed
1 task

Type error when passing Astro object to APIRoute #8514

minht11 opened this issue Sep 11, 2023 · 12 comments
Labels
needs triage Issue needs to be triaged

Comments

@minht11
Copy link
Contributor

minht11 commented Sep 11, 2023

Astro Info

Astro                    v3.0.12
Node                     v16.20.0
System                   Linux (x64)
Package Manager          unknown
Output                   server
Adapter                  @astrojs/node
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

Calling endpoint in the server and passing Astro object to APIRoute produces error

Argument of type 'Readonly<AstroGlobal<Record<string, any>, AstroComponentFactory, Record<string, string | undefined>>>' is not assignable to parameter of type 'APIContext<Record<string, any>, Record<string, string | undefined>>'.
  Property 'ResponseWithEncoding' is missing in type 'Readonly<AstroGlobal<Record<string, any>, AstroComponentFactory, Record<string, string | undefined>>>' but required in type 'APIContext<Record<string, any>, Record<string, string | undefined>>'.

whole example can be found in Astro [call-endpoints recipe](https://docs.astro.build/en/recipes/call-endpoints/

This is regression with Astro 3.0, 2.10 did not have this issue.
image

What's the expected result?

No type error.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-huqjmk?file=src%2Fpages%2Findex.astro

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Sep 11, 2023
@matthewp
Copy link
Contributor

matthewp commented Sep 11, 2023

Is there a property that's missing or not expected? That TS error isn't the greatest to read.

@Princesseuh
Copy link
Member

The type is hard to read because of the many generics that are unfortunately needed for editor tooling, but I think the error is simply that APIContext has a ResponseWithEncoding property that the Astro global doesn't have

@matthewp
Copy link
Contributor

Ah ok, we can probably add that.

@ematipico
Copy link
Member

Is it a correct usage of the route endpoints?

@matthewp
Copy link
Contributor

Yeah, after discussing this with some others, I'm not sure that we want to make it a breaking change if the API endpoint context types are unaligned with the Astro global. We do want to make them aligned when possible, but it shouldn't be a breaking change when they are not.

@artemszelenov
Copy link

I have the same issue. How to fix it?

@antheath
Copy link

@matthewp Is there a workaround to this issue? Do we need to downgrade to use API endpoints? This appears to be a breaking change.

@EddyVinck
Copy link

EddyVinck commented Oct 27, 2023

I "fixed" it by doing this:

let response = await GET({
  ...Astro,
  ResponseWithEncoding: Response,
  params: {
    id: post.data.id,
  },
});

But I am still getting an error when trying to do response.json() like the docs show:

Property 'json' does not exist on type 'EndpointOutput | Response'.
  Property 'json' does not exist on type '{ body: string; encoding?: BufferEncoding; }'
image

So I also have to do this:

const data = (await (response as Response).json()) as MyDataType;

@lnoss
Copy link

lnoss commented Dec 4, 2023

Meh, the only answer is types-torturing-casting? This is one of the examples showed in the documentation and it does just not work out of the box...

@lnoss
Copy link

lnoss commented Dec 4, 2023

Should be fixed in 4.0. No more torturing-casting with the current beta!
https://docs.astro.build/en/guides/upgrade-to/v4/#removed-returning-simple-objects-from-endpoints

@matthewp
Copy link
Contributor

matthewp commented Dec 4, 2023

It's working in 4.0 but note that we do not have tests that enforce that it doesn't break again. Since this is not an official feature yet, it's not guaranteed to work. But if someone wants to submit tests to check that it does, would likely accept!

@luiguild
Copy link

luiguild commented Dec 7, 2023

Still having this type problem.

The error message:

src/pages/[placeLang].astro:17:47 - error ts(2345): Argument of type 'Readonly<AstroGlobal<never, (_props: never) => any, Params>>' is not assignable to parameter of type 'APIContext<Record<string, any>, Record<string, string | undefined>>'.
  Types of property 'params' are incompatible.

The code:

const place: Place = await GET(Astro);

package.json

"dependencies": {
    "@astrojs/check": "^0.3.1",
    "@astrojs/vue": "^4.0.2",
    "astro": "^4.0.3",
    "typescript": "^5.3.2",
    "vue": "^3.2.30",
    "vue-i18n": "9"
  },

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

No branches or pull requests

9 participants