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(server$): config argument, optional GET, ServerError #6290

Merged
merged 22 commits into from
May 10, 2024

Conversation

PatrickJS
Copy link
Member

@PatrickJS PatrickJS commented May 9, 2024

Overview

fixes #5656
fixes #6288

Copy link

cloudflare-pages bot commented May 9, 2024

Deploying qwik-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: fac3aaa
Status: ✅  Deploy successful!
Preview URL: https://4077d306.qwik-8nx.pages.dev
Branch Preview URL: https://feat-server--options.qwik-8nx.pages.dev

View logs

@PatrickJS PatrickJS marked this pull request as ready for review May 10, 2024 04:49
@PatrickJS
Copy link
Member Author

in another pr I can add 3 levels of config. I also want to do this for better AbortSignal

@DustinJSilk
Copy link
Contributor

This is awesome! 🤩

Would it make sense to have ServerError take a generic argument so any serializable data can be returned?

export class ServerError<T = Record<any, any>> extends Error {
  constructor(
    public status: number,
    public data: T
  ) {
    super();
  }
}

Copy link
Member

@wmertens wmertens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking forward to this!

@wmertens
Copy link
Member

Misko is ok with this API extension

@PatrickJS
Copy link
Member Author

PatrickJS commented May 10, 2024

import { server$ } from "@builder.io/qwik-city";
import { ServerError } from "@builder.io/qwik-city/middleware/request-handler";

type ResponseTuple = [null | string, string];

const serverFunctionA = server$(async function a(): Promise<ResponseTuple> {
  throw new ServerError<[string]>(401, ["my error"]);
});

const serverFunctionB = server$(async function b(): Promise<ResponseTuple> {
  return [null, this.method || ""];
});

export const MultipleServerFunctionsInvokedInTask = component$(() => {
  const methodA = useSignal("");
  const methodB = useSignal("");

  useVisibleTask$(async () => {
    const [error /*, data */] = await serverFunctionA();
    if (error) {
      methodA.value = error;
    }
    await delay(1);
    //     err, method
    const [, method] = await serverFunctionB();
    methodB.value = method;
  });

  return (
    <div id="server-error">
      {methodA.value}
      {methodB.value}
    </div>
  );
});

@PatrickJS PatrickJS changed the title feat(server$): config argument feat(server$): config argument, optional GET, ServerError May 10, 2024
@PatrickJS PatrickJS merged commit 434737e into main May 10, 2024
24 checks passed
@PatrickJS PatrickJS deleted the feat-server$-options branch May 10, 2024 18:58
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.

[✨] Throw server$ errors with a specific status code [✨] Allow using GET with server$
3 participants