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

Api returns JSON.stringified values. #597

Closed
fmonper1 opened this issue Mar 17, 2024 · 2 comments
Closed

Api returns JSON.stringified values. #597

fmonper1 opened this issue Mar 17, 2024 · 2 comments

Comments

@fmonper1
Copy link

fmonper1 commented Mar 17, 2024

I recently updated a project form oazapfts v4 to v6 and since then every value returned from my API has te correct typescript types from my api, but the actual values are strings.

I've been able to work around it doing this dirty hack:

    const response = await createBusinessAndGetBucketUrlMutation.mutateAsync({
      organizationId,
      body: { ...data, fileNames: data.files.map((file) => file.name) },
    });

    // response has the correct Tpescript type, but throws a runtime error since its actually a string that can't be destructured as an object
    const { fileUrls, business } = JSON.parse(
      response.toString(),
    ) as typeof response; // typeof response has the correct type

Something must be wrong with the internal oazapfts.fetchJson function.

This is the generated code from my optimistc api:

export function createBusinessAndGetBucketUrl(
  {
    organizationId,
    body,
  }: {
    organizationId: string;
    body: {
      businessName: string;
      website: string;
      industry: string;
      type: string;
      fileNames: string[];
    };
  },
  opts?: Oazapfts.RequestOpts,
) {
  return oazapfts.ok(
    oazapfts.fetchJson<
      | {
          status: 200;
          data: {
            business: {
              id: string;
              backgroundImage: string | null;
              website: string | null;
              type: string | null;
              industry: string | null;
              contextReady: boolean;
              contextJobId: string | null;
              bgColor: string;
              businessName: string;
              organizationId: string;
              creatorId: string;
              createdAt: string;
              updatedAt: string;
            };
            fileUrls: string[];
          };
        }
      | {
          status: 400;
          data: {
            message: string;
          };
        }
    >(
      `/api/v2/business/organization/${encodeURIComponent(organizationId)}/`,
      oazapfts.json({
        ...opts,
        method: "POST",
        body,
      }),
    ),
  );
}

``

@GenerousSimon
Copy link

I ran into this issue too, had a look at the @oazapfts/runtime src (looking for JSON.parse), and you can see it is searching the headers for "json" in the Content-Type before choosing to JSON.parse it (or not). When I looked at my server responses more carefully I noticed "Content-Type": "text/plain", so changing my headers on the serverside to return "Content-Type": "application/json" fixed this for me - if that's helpful.

@Xiphe
Copy link
Collaborator

Xiphe commented Apr 22, 2024

Sorry for the late reply. And thanks for chiming in @GenerousSimon

@fmonper1 have you been able to check if this might be related to the response content type?

From looking at the code, this behavior had been there since v4, possibly earlier so this does not explain why the update broke your setup.

Could you please double-check if downgrading to v4 fixes the issue?

I'm closing here since I assume it's a problem with our server. Please re-open when someone can confirm it's oazapfts related.

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

3 participants