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: When closing over values in server$ the values should be marked as readonly #5238

Merged
merged 4 commits into from
May 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/qwik-city/runtime/src/server-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ import type {
} from './types';
import { useAction, useLocation, useQwikCityEnv } from './use-functions';
import { z } from 'zod';

import { isDev, isServer } from '@builder.io/qwik/build';

import type { FormSubmitCompletedDetail } from './form-component';

/** @public */
Expand Down Expand Up @@ -268,6 +270,16 @@ export const zodQrl = ((
/** @public */
export const zod$ = /*#__PURE__*/ implicit$FirstArg(zodQrl) as ZodConstructor;

const deepFreeze = (obj: any) => {
Object.getOwnPropertyNames(obj).forEach((prop) => {
const value = obj[prop];
if (value && typeof value === 'object') {
deepFreeze(value);
}
});
return Object.freeze(obj);
};

/** @public */
export const serverQrl = <T extends ServerFunction>(
qrl: QRL<T>,
Expand All @@ -292,9 +304,11 @@ export const serverQrl = <T extends ServerFunction>(
args.length > 0 && args[0] instanceof AbortSignal
? (args.shift() as AbortSignal)
: undefined;

if (isServer) {
// Running during SSR, we can call the function directly
let requestEvent = globalThis.qcAsyncRequestStore?.getStore() as RequestEvent | undefined;

if (!requestEvent) {
const contexts = [useQwikCityEnv()?.ev, this, _getContextEvent()] as RequestEvent[];
requestEvent = contexts.find(
Expand All @@ -304,7 +318,8 @@ export const serverQrl = <T extends ServerFunction>(
Object.prototype.hasOwnProperty.call(v, 'cookie')
);
}
return qrl.apply(requestEvent, args);

return qrl.apply(requestEvent, isDev ? deepFreeze(args) : args);
} else {
// Running on the client, we need to call the function via HTTP
const ctxElm = _getContextElement();
Expand Down
Loading