Make operations work with any (#2170)

This commit is contained in:
Filip Sodić 2024-07-11 20:35:21 +02:00 committed by GitHub
parent 3dcaf6bd81
commit 1aa139e87c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
50 changed files with 452 additions and 132 deletions

View File

@ -1,5 +1,6 @@
import { type Route } from "wasp/client";
import type {
IfAny,
_Awaited,
_ReturnType,
} from "wasp/universal/types"
@ -55,11 +56,11 @@ export type QueryMetadata = {
*/
export type OperationRpcFor<BackendOperation extends GenericBackendOperation> =
Parameters<BackendOperation> extends []
? ClientOperation<void, _Awaited<_ReturnType<BackendOperation>>>
: ClientOperation<
Parameters<BackendOperation>[0],
_Awaited<_ReturnType<BackendOperation>>
>
? ClientOperation<void, _Awaited<_ReturnType<BackendOperation>>>
: ClientOperation<
Parameters<BackendOperation>[0],
_Awaited<_ReturnType<BackendOperation>>
>
// PRIVATE API (needed in SDK)
/**
@ -74,8 +75,21 @@ export type GenericBackendOperation = (args: never, context: any) => unknown
*/
export type GenericOperationRpc = (args: never) => Promise<unknown>
// NOTE: There's some duplication in the below types.
// Read the discussion here to understand why before trying to remove it:
// https://github.com/wasp-lang/wasp/pull/2170#discussion_r1671285049
//
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/2170#issue-2398830273
type ClientOperation<Input, Output> =
IfAny<
Input,
(args?: any) => Promise<Output>,
ClientOperationWithNonAnyInput<Input, Output>
>
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/1090#discussion_r1159732471
type ClientOperation<Input, Output> = [Input] extends [never]
type ClientOperationWithNonAnyInput<Input, Output> =
[Input] extends [never]
? (args?: unknown) => Promise<Output>
: [Input] extends [void]
? () => Promise<Output>

View File

@ -1,5 +1,5 @@
{{={= =}=}}
import { _Awaited, _ReturnType } from '../../universal/types'
import { IfAny, _Awaited, _ReturnType } from '../../universal/types'
{=# isAuthEnabled =}
import { type AuthUser } from 'wasp/auth'
@ -160,6 +160,9 @@ function includesPayload<Input>(
type AuthenticatedOperationArgsFor<Op extends GenericAuthenticatedOperationDefinition> =
Parameters<AuthenticatedOperationFor<Op>>
// NOTE: There's some duplication in the below types.
// Read the discussion here to understand why before attempting to remove it:
// https://github.com/wasp-lang/wasp/pull/2170#discussion_r1671285049
/**
* Constructs the type for an authenticated operation's server-side API.
*
@ -168,6 +171,14 @@ type AuthenticatedOperationArgsFor<Op extends GenericAuthenticatedOperationDefin
* @template Output The type of the operation's return value.
*/
type AuthenticatedOperation<Input, Output> =
IfAny<
Input,
(args: any, context: { user: AuthUser }) => Promise<Output>,
AuthenticatedOperationWithNonAnyInput<Input, Output>
>
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/1090#discussion_r1159732471
type AuthenticatedOperationWithNonAnyInput<Input, Output> =
[Input] extends [never]
? (args: unknown, context: { user: AuthUser }) => Promise<Output>
: [Input] extends [void]
@ -187,6 +198,7 @@ type GenericAuthenticatedOperationDefinition = AuthenticatedOperationDefinition<
>
{=/ isAuthEnabled =}
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/2170#issue-2398830273
/**
* Constructs the type for an unauthenticated operation's server-side API.
*
@ -195,13 +207,20 @@ type GenericAuthenticatedOperationDefinition = AuthenticatedOperationDefinition<
* @template Output The type of the operation's return value.
*/
type UnauthenticatedOperation<Input, Output> =
IfAny<
Input,
(args?: any) => Promise<Output>,
UnauthenticatedOperationWithNonAnyInput<Input, Output>
>
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/1090#discussion_r1159732471
type UnauthenticatedOperationWithNonAnyInput<Input, Output> =
[Input] extends [never]
? (args: unknown) => Promise<Output>
? (args?: unknown) => Promise<Output>
: [Input] extends [void]
? () => Promise<Output>
: (args: Input) => Promise<Output>
/**
* The principal type for an unauthenticated operation's definition (i.e., all
* unauthenticated operation definition types are a subtype of this type).

View File

@ -27,9 +27,13 @@ export type _Awaited<T> = T extends Promise<infer V>
// TODO: investigate how to properly specify the 'extends' constraint for function
// type (i.e., any vs never and unknown) and stick with that. Take DX into
// consideration.
export type _ReturnType<T extends (...args: never[]) => unknown> =
export type _ReturnType<T extends (...args: never[]) => unknown> =
T extends (...args: never[]) => infer R ? R : never
// Returns elements of an array except the first one.
export type Tail<T extends [unknown, ...unknown[]]> = T extends [unknown, ...infer R] ? R : never;
// Source: https://stackoverflow.com/a/55541672
export type IfAny<Value, Then, Else> = 0 extends (1 & Value) ? Then : Else;

View File

@ -102,7 +102,7 @@
"file",
"../out/sdk/wasp/client/operations/rpc.ts"
],
"63fb0670b2ccff4ad005bbb76597572a60e01f31b739d7bc764bb59c4f3c53d4"
"08f30fe4e31a691a225949ccd4d638cffe78bf2f4f2a76feb70133d4d9a5ba94"
],
[
[
@ -305,7 +305,7 @@
"file",
"../out/sdk/wasp/server/operations/wrappers.ts"
],
"4f3ffd00d845664a0122a24d2a12279c0febd744214c7167e7d9dea41330e107"
"8e38950b0929b8fbe662dd796c4c445285f91b37df3d40b83b4685347e5aa3aa"
],
[
[
@ -333,7 +333,7 @@
"file",
"../out/sdk/wasp/universal/types.ts"
],
"791cb40cb5c405ade34d46770a8178353257e8d2351a877b6ca5e88aafb0ec13"
"be0d749bf9ae2f44ca2c281e0fb65c4d30c1d07e375ba38407e6c4fa1f05cc87"
],
[
[

View File

@ -1,5 +1,6 @@
import { type Route } from "wasp/client";
import type {
IfAny,
_Awaited,
_ReturnType,
} from "wasp/universal/types"
@ -55,11 +56,11 @@ export type QueryMetadata = {
*/
export type OperationRpcFor<BackendOperation extends GenericBackendOperation> =
Parameters<BackendOperation> extends []
? ClientOperation<void, _Awaited<_ReturnType<BackendOperation>>>
: ClientOperation<
Parameters<BackendOperation>[0],
_Awaited<_ReturnType<BackendOperation>>
>
? ClientOperation<void, _Awaited<_ReturnType<BackendOperation>>>
: ClientOperation<
Parameters<BackendOperation>[0],
_Awaited<_ReturnType<BackendOperation>>
>
// PRIVATE API (needed in SDK)
/**
@ -74,8 +75,21 @@ export type GenericBackendOperation = (args: never, context: any) => unknown
*/
export type GenericOperationRpc = (args: never) => Promise<unknown>
// NOTE: There's some duplication in the below types.
// Read the discussion here to understand why before trying to remove it:
// https://github.com/wasp-lang/wasp/pull/2170#discussion_r1671285049
//
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/2170#issue-2398830273
type ClientOperation<Input, Output> =
IfAny<
Input,
(args?: any) => Promise<Output>,
ClientOperationWithNonAnyInput<Input, Output>
>
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/1090#discussion_r1159732471
type ClientOperation<Input, Output> = [Input] extends [never]
type ClientOperationWithNonAnyInput<Input, Output> =
[Input] extends [never]
? (args?: unknown) => Promise<Output>
: [Input] extends [void]
? () => Promise<Output>

View File

@ -1,5 +1,5 @@
import { type Route } from "wasp/client";
import type { _Awaited, _ReturnType } from "wasp/universal/types";
import type { IfAny, _Awaited, _ReturnType } from "wasp/universal/types";
/**
* The client Query object type. It's a callable Query function with some extra
* properties (metadata).
@ -34,5 +34,8 @@ export type GenericBackendOperation = (args: never, context: any) => unknown;
* A supertype of all possible frontend RPC function types.
*/
export type GenericOperationRpc = (args: never) => Promise<unknown>;
type ClientOperation<Input, Output> = [Input] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
type ClientOperation<Input, Output> = IfAny<Input, (args?: any) => Promise<Output>, ClientOperationWithNonAnyInput<Input, Output>>;
type ClientOperationWithNonAnyInput<Input, Output> = [
Input
] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
export {};

View File

@ -1,4 +1,4 @@
import { _Awaited, _ReturnType } from '../../universal/types';
import { IfAny, _Awaited, _ReturnType } from '../../universal/types';
import { _Entity, UnauthenticatedOperationDefinition, Payload } from '../_types';
/**
* Constructs the unauthenticated operation's server-side API type from its
@ -24,9 +24,10 @@ export declare function createUnauthenticatedOperation<OperationDefinition exten
* `void` if the operation doesn't expect a payload).
* @template Output The type of the operation's return value.
*/
type UnauthenticatedOperation<Input, Output> = [
type UnauthenticatedOperation<Input, Output> = IfAny<Input, (args?: any) => Promise<Output>, UnauthenticatedOperationWithNonAnyInput<Input, Output>>;
type UnauthenticatedOperationWithNonAnyInput<Input, Output> = [
Input
] extends [never] ? (args: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
/**
* The principal type for an unauthenticated operation's definition (i.e., all
* unauthenticated operation definition types are a subtype of this type).

View File

@ -4,3 +4,4 @@ export type Expand<T> = T extends (...args: infer A) => infer R ? (...args: A) =
export type _Awaited<T> = T extends Promise<infer V> ? _Awaited<V> : T;
export type _ReturnType<T extends (...args: never[]) => unknown> = T extends (...args: never[]) => infer R ? R : never;
export type Tail<T extends [unknown, ...unknown[]]> = T extends [unknown, ...infer R] ? R : never;
export type IfAny<Value, Then, Else> = 0 extends (1 & Value) ? Then : Else;

View File

@ -1,4 +1,4 @@
import { _Awaited, _ReturnType } from '../../universal/types'
import { IfAny, _Awaited, _ReturnType } from '../../universal/types'
import {
_Entity,
@ -54,6 +54,7 @@ export function createUnauthenticatedOperation<
return operation as UnauthenticatedOperationFor<OperationDefinition>
}
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/2170#issue-2398830273
/**
* Constructs the type for an unauthenticated operation's server-side API.
*
@ -62,13 +63,20 @@ export function createUnauthenticatedOperation<
* @template Output The type of the operation's return value.
*/
type UnauthenticatedOperation<Input, Output> =
IfAny<
Input,
(args?: any) => Promise<Output>,
UnauthenticatedOperationWithNonAnyInput<Input, Output>
>
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/1090#discussion_r1159732471
type UnauthenticatedOperationWithNonAnyInput<Input, Output> =
[Input] extends [never]
? (args: unknown) => Promise<Output>
? (args?: unknown) => Promise<Output>
: [Input] extends [void]
? () => Promise<Output>
: (args: Input) => Promise<Output>
/**
* The principal type for an unauthenticated operation's definition (i.e., all
* unauthenticated operation definition types are a subtype of this type).

View File

@ -27,9 +27,13 @@ export type _Awaited<T> = T extends Promise<infer V>
// TODO: investigate how to properly specify the 'extends' constraint for function
// type (i.e., any vs never and unknown) and stick with that. Take DX into
// consideration.
export type _ReturnType<T extends (...args: never[]) => unknown> =
export type _ReturnType<T extends (...args: never[]) => unknown> =
T extends (...args: never[]) => infer R ? R : never
// Returns elements of an array except the first one.
export type Tail<T extends [unknown, ...unknown[]]> = T extends [unknown, ...infer R] ? R : never;
// Source: https://stackoverflow.com/a/55541672
export type IfAny<Value, Then, Else> = 0 extends (1 & Value) ? Then : Else;

View File

@ -1,5 +1,6 @@
import { type Route } from "wasp/client";
import type {
IfAny,
_Awaited,
_ReturnType,
} from "wasp/universal/types"
@ -55,11 +56,11 @@ export type QueryMetadata = {
*/
export type OperationRpcFor<BackendOperation extends GenericBackendOperation> =
Parameters<BackendOperation> extends []
? ClientOperation<void, _Awaited<_ReturnType<BackendOperation>>>
: ClientOperation<
Parameters<BackendOperation>[0],
_Awaited<_ReturnType<BackendOperation>>
>
? ClientOperation<void, _Awaited<_ReturnType<BackendOperation>>>
: ClientOperation<
Parameters<BackendOperation>[0],
_Awaited<_ReturnType<BackendOperation>>
>
// PRIVATE API (needed in SDK)
/**
@ -74,8 +75,21 @@ export type GenericBackendOperation = (args: never, context: any) => unknown
*/
export type GenericOperationRpc = (args: never) => Promise<unknown>
// NOTE: There's some duplication in the below types.
// Read the discussion here to understand why before trying to remove it:
// https://github.com/wasp-lang/wasp/pull/2170#discussion_r1671285049
//
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/2170#issue-2398830273
type ClientOperation<Input, Output> =
IfAny<
Input,
(args?: any) => Promise<Output>,
ClientOperationWithNonAnyInput<Input, Output>
>
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/1090#discussion_r1159732471
type ClientOperation<Input, Output> = [Input] extends [never]
type ClientOperationWithNonAnyInput<Input, Output> =
[Input] extends [never]
? (args?: unknown) => Promise<Output>
: [Input] extends [void]
? () => Promise<Output>

View File

@ -1,5 +1,5 @@
import { type Route } from "wasp/client";
import type { _Awaited, _ReturnType } from "wasp/universal/types";
import type { IfAny, _Awaited, _ReturnType } from "wasp/universal/types";
/**
* The client Query object type. It's a callable Query function with some extra
* properties (metadata).
@ -34,5 +34,8 @@ export type GenericBackendOperation = (args: never, context: any) => unknown;
* A supertype of all possible frontend RPC function types.
*/
export type GenericOperationRpc = (args: never) => Promise<unknown>;
type ClientOperation<Input, Output> = [Input] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
type ClientOperation<Input, Output> = IfAny<Input, (args?: any) => Promise<Output>, ClientOperationWithNonAnyInput<Input, Output>>;
type ClientOperationWithNonAnyInput<Input, Output> = [
Input
] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
export {};

View File

@ -1,4 +1,4 @@
import { _Awaited, _ReturnType } from '../../universal/types';
import { IfAny, _Awaited, _ReturnType } from '../../universal/types';
import { _Entity, UnauthenticatedOperationDefinition, Payload } from '../_types';
/**
* Constructs the unauthenticated operation's server-side API type from its
@ -24,9 +24,10 @@ export declare function createUnauthenticatedOperation<OperationDefinition exten
* `void` if the operation doesn't expect a payload).
* @template Output The type of the operation's return value.
*/
type UnauthenticatedOperation<Input, Output> = [
type UnauthenticatedOperation<Input, Output> = IfAny<Input, (args?: any) => Promise<Output>, UnauthenticatedOperationWithNonAnyInput<Input, Output>>;
type UnauthenticatedOperationWithNonAnyInput<Input, Output> = [
Input
] extends [never] ? (args: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
/**
* The principal type for an unauthenticated operation's definition (i.e., all
* unauthenticated operation definition types are a subtype of this type).

View File

@ -4,3 +4,4 @@ export type Expand<T> = T extends (...args: infer A) => infer R ? (...args: A) =
export type _Awaited<T> = T extends Promise<infer V> ? _Awaited<V> : T;
export type _ReturnType<T extends (...args: never[]) => unknown> = T extends (...args: never[]) => infer R ? R : never;
export type Tail<T extends [unknown, ...unknown[]]> = T extends [unknown, ...infer R] ? R : never;
export type IfAny<Value, Then, Else> = 0 extends (1 & Value) ? Then : Else;

View File

@ -1,4 +1,4 @@
import { _Awaited, _ReturnType } from '../../universal/types'
import { IfAny, _Awaited, _ReturnType } from '../../universal/types'
import {
_Entity,
@ -54,6 +54,7 @@ export function createUnauthenticatedOperation<
return operation as UnauthenticatedOperationFor<OperationDefinition>
}
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/2170#issue-2398830273
/**
* Constructs the type for an unauthenticated operation's server-side API.
*
@ -62,13 +63,20 @@ export function createUnauthenticatedOperation<
* @template Output The type of the operation's return value.
*/
type UnauthenticatedOperation<Input, Output> =
IfAny<
Input,
(args?: any) => Promise<Output>,
UnauthenticatedOperationWithNonAnyInput<Input, Output>
>
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/1090#discussion_r1159732471
type UnauthenticatedOperationWithNonAnyInput<Input, Output> =
[Input] extends [never]
? (args: unknown) => Promise<Output>
? (args?: unknown) => Promise<Output>
: [Input] extends [void]
? () => Promise<Output>
: (args: Input) => Promise<Output>
/**
* The principal type for an unauthenticated operation's definition (i.e., all
* unauthenticated operation definition types are a subtype of this type).

View File

@ -27,9 +27,13 @@ export type _Awaited<T> = T extends Promise<infer V>
// TODO: investigate how to properly specify the 'extends' constraint for function
// type (i.e., any vs never and unknown) and stick with that. Take DX into
// consideration.
export type _ReturnType<T extends (...args: never[]) => unknown> =
export type _ReturnType<T extends (...args: never[]) => unknown> =
T extends (...args: never[]) => infer R ? R : never
// Returns elements of an array except the first one.
export type Tail<T extends [unknown, ...unknown[]]> = T extends [unknown, ...infer R] ? R : never;
// Source: https://stackoverflow.com/a/55541672
export type IfAny<Value, Then, Else> = 0 extends (1 & Value) ? Then : Else;

View File

@ -102,7 +102,7 @@
"file",
"../out/sdk/wasp/client/operations/rpc.ts"
],
"63fb0670b2ccff4ad005bbb76597572a60e01f31b739d7bc764bb59c4f3c53d4"
"08f30fe4e31a691a225949ccd4d638cffe78bf2f4f2a76feb70133d4d9a5ba94"
],
[
[
@ -305,7 +305,7 @@
"file",
"../out/sdk/wasp/server/operations/wrappers.ts"
],
"4f3ffd00d845664a0122a24d2a12279c0febd744214c7167e7d9dea41330e107"
"8e38950b0929b8fbe662dd796c4c445285f91b37df3d40b83b4685347e5aa3aa"
],
[
[
@ -333,7 +333,7 @@
"file",
"../out/sdk/wasp/universal/types.ts"
],
"791cb40cb5c405ade34d46770a8178353257e8d2351a877b6ca5e88aafb0ec13"
"be0d749bf9ae2f44ca2c281e0fb65c4d30c1d07e375ba38407e6c4fa1f05cc87"
],
[
[

View File

@ -1,5 +1,6 @@
import { type Route } from "wasp/client";
import type {
IfAny,
_Awaited,
_ReturnType,
} from "wasp/universal/types"
@ -55,11 +56,11 @@ export type QueryMetadata = {
*/
export type OperationRpcFor<BackendOperation extends GenericBackendOperation> =
Parameters<BackendOperation> extends []
? ClientOperation<void, _Awaited<_ReturnType<BackendOperation>>>
: ClientOperation<
Parameters<BackendOperation>[0],
_Awaited<_ReturnType<BackendOperation>>
>
? ClientOperation<void, _Awaited<_ReturnType<BackendOperation>>>
: ClientOperation<
Parameters<BackendOperation>[0],
_Awaited<_ReturnType<BackendOperation>>
>
// PRIVATE API (needed in SDK)
/**
@ -74,8 +75,21 @@ export type GenericBackendOperation = (args: never, context: any) => unknown
*/
export type GenericOperationRpc = (args: never) => Promise<unknown>
// NOTE: There's some duplication in the below types.
// Read the discussion here to understand why before trying to remove it:
// https://github.com/wasp-lang/wasp/pull/2170#discussion_r1671285049
//
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/2170#issue-2398830273
type ClientOperation<Input, Output> =
IfAny<
Input,
(args?: any) => Promise<Output>,
ClientOperationWithNonAnyInput<Input, Output>
>
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/1090#discussion_r1159732471
type ClientOperation<Input, Output> = [Input] extends [never]
type ClientOperationWithNonAnyInput<Input, Output> =
[Input] extends [never]
? (args?: unknown) => Promise<Output>
: [Input] extends [void]
? () => Promise<Output>

View File

@ -1,5 +1,5 @@
import { type Route } from "wasp/client";
import type { _Awaited, _ReturnType } from "wasp/universal/types";
import type { IfAny, _Awaited, _ReturnType } from "wasp/universal/types";
/**
* The client Query object type. It's a callable Query function with some extra
* properties (metadata).
@ -34,5 +34,8 @@ export type GenericBackendOperation = (args: never, context: any) => unknown;
* A supertype of all possible frontend RPC function types.
*/
export type GenericOperationRpc = (args: never) => Promise<unknown>;
type ClientOperation<Input, Output> = [Input] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
type ClientOperation<Input, Output> = IfAny<Input, (args?: any) => Promise<Output>, ClientOperationWithNonAnyInput<Input, Output>>;
type ClientOperationWithNonAnyInput<Input, Output> = [
Input
] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
export {};

View File

@ -1,4 +1,4 @@
import { _Awaited, _ReturnType } from '../../universal/types';
import { IfAny, _Awaited, _ReturnType } from '../../universal/types';
import { _Entity, UnauthenticatedOperationDefinition, Payload } from '../_types';
/**
* Constructs the unauthenticated operation's server-side API type from its
@ -24,9 +24,10 @@ export declare function createUnauthenticatedOperation<OperationDefinition exten
* `void` if the operation doesn't expect a payload).
* @template Output The type of the operation's return value.
*/
type UnauthenticatedOperation<Input, Output> = [
type UnauthenticatedOperation<Input, Output> = IfAny<Input, (args?: any) => Promise<Output>, UnauthenticatedOperationWithNonAnyInput<Input, Output>>;
type UnauthenticatedOperationWithNonAnyInput<Input, Output> = [
Input
] extends [never] ? (args: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
/**
* The principal type for an unauthenticated operation's definition (i.e., all
* unauthenticated operation definition types are a subtype of this type).

View File

@ -4,3 +4,4 @@ export type Expand<T> = T extends (...args: infer A) => infer R ? (...args: A) =
export type _Awaited<T> = T extends Promise<infer V> ? _Awaited<V> : T;
export type _ReturnType<T extends (...args: never[]) => unknown> = T extends (...args: never[]) => infer R ? R : never;
export type Tail<T extends [unknown, ...unknown[]]> = T extends [unknown, ...infer R] ? R : never;
export type IfAny<Value, Then, Else> = 0 extends (1 & Value) ? Then : Else;

View File

@ -1,4 +1,4 @@
import { _Awaited, _ReturnType } from '../../universal/types'
import { IfAny, _Awaited, _ReturnType } from '../../universal/types'
import {
_Entity,
@ -54,6 +54,7 @@ export function createUnauthenticatedOperation<
return operation as UnauthenticatedOperationFor<OperationDefinition>
}
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/2170#issue-2398830273
/**
* Constructs the type for an unauthenticated operation's server-side API.
*
@ -62,13 +63,20 @@ export function createUnauthenticatedOperation<
* @template Output The type of the operation's return value.
*/
type UnauthenticatedOperation<Input, Output> =
IfAny<
Input,
(args?: any) => Promise<Output>,
UnauthenticatedOperationWithNonAnyInput<Input, Output>
>
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/1090#discussion_r1159732471
type UnauthenticatedOperationWithNonAnyInput<Input, Output> =
[Input] extends [never]
? (args: unknown) => Promise<Output>
? (args?: unknown) => Promise<Output>
: [Input] extends [void]
? () => Promise<Output>
: (args: Input) => Promise<Output>
/**
* The principal type for an unauthenticated operation's definition (i.e., all
* unauthenticated operation definition types are a subtype of this type).

View File

@ -27,9 +27,13 @@ export type _Awaited<T> = T extends Promise<infer V>
// TODO: investigate how to properly specify the 'extends' constraint for function
// type (i.e., any vs never and unknown) and stick with that. Take DX into
// consideration.
export type _ReturnType<T extends (...args: never[]) => unknown> =
export type _ReturnType<T extends (...args: never[]) => unknown> =
T extends (...args: never[]) => infer R ? R : never
// Returns elements of an array except the first one.
export type Tail<T extends [unknown, ...unknown[]]> = T extends [unknown, ...infer R] ? R : never;
// Source: https://stackoverflow.com/a/55541672
export type IfAny<Value, Then, Else> = 0 extends (1 & Value) ? Then : Else;

View File

@ -305,7 +305,7 @@
"file",
"../out/sdk/wasp/client/operations/rpc.ts"
],
"63fb0670b2ccff4ad005bbb76597572a60e01f31b739d7bc764bb59c4f3c53d4"
"08f30fe4e31a691a225949ccd4d638cffe78bf2f4f2a76feb70133d4d9a5ba94"
],
[
[
@ -725,7 +725,7 @@
"file",
"../out/sdk/wasp/server/operations/wrappers.ts"
],
"d2ae69bd910aa1238c6f87a6d4f6ca88e187935059820a8bd3860cbc9f5e1d3b"
"3ca438a1f9e44d7391de91c6e923d19e638932d079922a73b6d4d683e0509f11"
],
[
[
@ -753,7 +753,7 @@
"file",
"../out/sdk/wasp/universal/types.ts"
],
"791cb40cb5c405ade34d46770a8178353257e8d2351a877b6ca5e88aafb0ec13"
"be0d749bf9ae2f44ca2c281e0fb65c4d30c1d07e375ba38407e6c4fa1f05cc87"
],
[
[

View File

@ -1,5 +1,6 @@
import { type Route } from "wasp/client";
import type {
IfAny,
_Awaited,
_ReturnType,
} from "wasp/universal/types"
@ -55,11 +56,11 @@ export type QueryMetadata = {
*/
export type OperationRpcFor<BackendOperation extends GenericBackendOperation> =
Parameters<BackendOperation> extends []
? ClientOperation<void, _Awaited<_ReturnType<BackendOperation>>>
: ClientOperation<
Parameters<BackendOperation>[0],
_Awaited<_ReturnType<BackendOperation>>
>
? ClientOperation<void, _Awaited<_ReturnType<BackendOperation>>>
: ClientOperation<
Parameters<BackendOperation>[0],
_Awaited<_ReturnType<BackendOperation>>
>
// PRIVATE API (needed in SDK)
/**
@ -74,8 +75,21 @@ export type GenericBackendOperation = (args: never, context: any) => unknown
*/
export type GenericOperationRpc = (args: never) => Promise<unknown>
// NOTE: There's some duplication in the below types.
// Read the discussion here to understand why before trying to remove it:
// https://github.com/wasp-lang/wasp/pull/2170#discussion_r1671285049
//
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/2170#issue-2398830273
type ClientOperation<Input, Output> =
IfAny<
Input,
(args?: any) => Promise<Output>,
ClientOperationWithNonAnyInput<Input, Output>
>
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/1090#discussion_r1159732471
type ClientOperation<Input, Output> = [Input] extends [never]
type ClientOperationWithNonAnyInput<Input, Output> =
[Input] extends [never]
? (args?: unknown) => Promise<Output>
: [Input] extends [void]
? () => Promise<Output>

View File

@ -2,4 +2,4 @@ import type { Query, Action } from "../operations/rpc";
export declare function makeUseQueryFor<Input, Output>(query: Query<Input, Output>): (queryFnArgs?: Input, options?: any) => import("@tanstack/react-query").UseQueryResult<Output, Error>;
export declare function makeUseActionFor<Input = unknown, Output = unknown>(action: Action<Input, Output>): (actionOptions?: {
optimisticUpdates: import("../operations").OptimisticUpdateDefinition<Input, any>[];
}) => [Input] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
}) => import("../../universal/types").IfAny<Input, (args?: any) => Promise<Output>, [Input] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>>;

View File

@ -1,5 +1,5 @@
import { type Route } from "wasp/client";
import type { _Awaited, _ReturnType } from "wasp/universal/types";
import type { IfAny, _Awaited, _ReturnType } from "wasp/universal/types";
/**
* The client Query object type. It's a callable Query function with some extra
* properties (metadata).
@ -34,5 +34,8 @@ export type GenericBackendOperation = (args: never, context: any) => unknown;
* A supertype of all possible frontend RPC function types.
*/
export type GenericOperationRpc = (args: never) => Promise<unknown>;
type ClientOperation<Input, Output> = [Input] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
type ClientOperation<Input, Output> = IfAny<Input, (args?: any) => Promise<Output>, ClientOperationWithNonAnyInput<Input, Output>>;
type ClientOperationWithNonAnyInput<Input, Output> = [
Input
] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
export {};

View File

@ -1,4 +1,4 @@
import { _Awaited, _ReturnType } from '../../universal/types';
import { IfAny, _Awaited, _ReturnType } from '../../universal/types';
import { type AuthUser } from 'wasp/auth';
import { _Entity, AuthenticatedOperationDefinition, UnauthenticatedOperationDefinition, Payload } from '../_types';
/**
@ -49,7 +49,10 @@ export declare function createAuthenticatedOperation<OperationDefinition extends
* `void` if the operation doesn't expect a payload).
* @template Output The type of the operation's return value.
*/
type AuthenticatedOperation<Input, Output> = [
type AuthenticatedOperation<Input, Output> = IfAny<Input, (args: any, context: {
user: AuthUser;
}) => Promise<Output>, AuthenticatedOperationWithNonAnyInput<Input, Output>>;
type AuthenticatedOperationWithNonAnyInput<Input, Output> = [
Input
] extends [never] ? (args: unknown, context: {
user: AuthUser;
@ -71,9 +74,10 @@ type GenericAuthenticatedOperationDefinition = AuthenticatedOperationDefinition<
* `void` if the operation doesn't expect a payload).
* @template Output The type of the operation's return value.
*/
type UnauthenticatedOperation<Input, Output> = [
type UnauthenticatedOperation<Input, Output> = IfAny<Input, (args?: any) => Promise<Output>, UnauthenticatedOperationWithNonAnyInput<Input, Output>>;
type UnauthenticatedOperationWithNonAnyInput<Input, Output> = [
Input
] extends [never] ? (args: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
/**
* The principal type for an unauthenticated operation's definition (i.e., all
* unauthenticated operation definition types are a subtype of this type).

View File

@ -4,3 +4,4 @@ export type Expand<T> = T extends (...args: infer A) => infer R ? (...args: A) =
export type _Awaited<T> = T extends Promise<infer V> ? _Awaited<V> : T;
export type _ReturnType<T extends (...args: never[]) => unknown> = T extends (...args: never[]) => infer R ? R : never;
export type Tail<T extends [unknown, ...unknown[]]> = T extends [unknown, ...infer R] ? R : never;
export type IfAny<Value, Then, Else> = 0 extends (1 & Value) ? Then : Else;

View File

@ -1,4 +1,4 @@
import { _Awaited, _ReturnType } from '../../universal/types'
import { IfAny, _Awaited, _ReturnType } from '../../universal/types'
import { type AuthUser } from 'wasp/auth'
import {
@ -154,6 +154,9 @@ function includesPayload<Input>(
type AuthenticatedOperationArgsFor<Op extends GenericAuthenticatedOperationDefinition> =
Parameters<AuthenticatedOperationFor<Op>>
// NOTE: There's some duplication in the below types.
// Read the discussion here to understand why before attempting to remove it:
// https://github.com/wasp-lang/wasp/pull/2170#discussion_r1671285049
/**
* Constructs the type for an authenticated operation's server-side API.
*
@ -162,6 +165,14 @@ type AuthenticatedOperationArgsFor<Op extends GenericAuthenticatedOperationDefin
* @template Output The type of the operation's return value.
*/
type AuthenticatedOperation<Input, Output> =
IfAny<
Input,
(args: any, context: { user: AuthUser }) => Promise<Output>,
AuthenticatedOperationWithNonAnyInput<Input, Output>
>
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/1090#discussion_r1159732471
type AuthenticatedOperationWithNonAnyInput<Input, Output> =
[Input] extends [never]
? (args: unknown, context: { user: AuthUser }) => Promise<Output>
: [Input] extends [void]
@ -180,6 +191,7 @@ type GenericAuthenticatedOperationDefinition = AuthenticatedOperationDefinition<
Payload
>
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/2170#issue-2398830273
/**
* Constructs the type for an unauthenticated operation's server-side API.
*
@ -188,13 +200,20 @@ type GenericAuthenticatedOperationDefinition = AuthenticatedOperationDefinition<
* @template Output The type of the operation's return value.
*/
type UnauthenticatedOperation<Input, Output> =
IfAny<
Input,
(args?: any) => Promise<Output>,
UnauthenticatedOperationWithNonAnyInput<Input, Output>
>
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/1090#discussion_r1159732471
type UnauthenticatedOperationWithNonAnyInput<Input, Output> =
[Input] extends [never]
? (args: unknown) => Promise<Output>
? (args?: unknown) => Promise<Output>
: [Input] extends [void]
? () => Promise<Output>
: (args: Input) => Promise<Output>
/**
* The principal type for an unauthenticated operation's definition (i.e., all
* unauthenticated operation definition types are a subtype of this type).

View File

@ -27,9 +27,13 @@ export type _Awaited<T> = T extends Promise<infer V>
// TODO: investigate how to properly specify the 'extends' constraint for function
// type (i.e., any vs never and unknown) and stick with that. Take DX into
// consideration.
export type _ReturnType<T extends (...args: never[]) => unknown> =
export type _ReturnType<T extends (...args: never[]) => unknown> =
T extends (...args: never[]) => infer R ? R : never
// Returns elements of an array except the first one.
export type Tail<T extends [unknown, ...unknown[]]> = T extends [unknown, ...infer R] ? R : never;
// Source: https://stackoverflow.com/a/55541672
export type IfAny<Value, Then, Else> = 0 extends (1 & Value) ? Then : Else;

View File

@ -102,7 +102,7 @@
"file",
"../out/sdk/wasp/client/operations/rpc.ts"
],
"63fb0670b2ccff4ad005bbb76597572a60e01f31b739d7bc764bb59c4f3c53d4"
"08f30fe4e31a691a225949ccd4d638cffe78bf2f4f2a76feb70133d4d9a5ba94"
],
[
[
@ -361,7 +361,7 @@
"file",
"../out/sdk/wasp/server/operations/wrappers.ts"
],
"4f3ffd00d845664a0122a24d2a12279c0febd744214c7167e7d9dea41330e107"
"8e38950b0929b8fbe662dd796c4c445285f91b37df3d40b83b4685347e5aa3aa"
],
[
[
@ -389,7 +389,7 @@
"file",
"../out/sdk/wasp/universal/types.ts"
],
"791cb40cb5c405ade34d46770a8178353257e8d2351a877b6ca5e88aafb0ec13"
"be0d749bf9ae2f44ca2c281e0fb65c4d30c1d07e375ba38407e6c4fa1f05cc87"
],
[
[

View File

@ -1,5 +1,6 @@
import { type Route } from "wasp/client";
import type {
IfAny,
_Awaited,
_ReturnType,
} from "wasp/universal/types"
@ -55,11 +56,11 @@ export type QueryMetadata = {
*/
export type OperationRpcFor<BackendOperation extends GenericBackendOperation> =
Parameters<BackendOperation> extends []
? ClientOperation<void, _Awaited<_ReturnType<BackendOperation>>>
: ClientOperation<
Parameters<BackendOperation>[0],
_Awaited<_ReturnType<BackendOperation>>
>
? ClientOperation<void, _Awaited<_ReturnType<BackendOperation>>>
: ClientOperation<
Parameters<BackendOperation>[0],
_Awaited<_ReturnType<BackendOperation>>
>
// PRIVATE API (needed in SDK)
/**
@ -74,8 +75,21 @@ export type GenericBackendOperation = (args: never, context: any) => unknown
*/
export type GenericOperationRpc = (args: never) => Promise<unknown>
// NOTE: There's some duplication in the below types.
// Read the discussion here to understand why before trying to remove it:
// https://github.com/wasp-lang/wasp/pull/2170#discussion_r1671285049
//
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/2170#issue-2398830273
type ClientOperation<Input, Output> =
IfAny<
Input,
(args?: any) => Promise<Output>,
ClientOperationWithNonAnyInput<Input, Output>
>
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/1090#discussion_r1159732471
type ClientOperation<Input, Output> = [Input] extends [never]
type ClientOperationWithNonAnyInput<Input, Output> =
[Input] extends [never]
? (args?: unknown) => Promise<Output>
: [Input] extends [void]
? () => Promise<Output>

View File

@ -1,5 +1,5 @@
import { type Route } from "wasp/client";
import type { _Awaited, _ReturnType } from "wasp/universal/types";
import type { IfAny, _Awaited, _ReturnType } from "wasp/universal/types";
/**
* The client Query object type. It's a callable Query function with some extra
* properties (metadata).
@ -34,5 +34,8 @@ export type GenericBackendOperation = (args: never, context: any) => unknown;
* A supertype of all possible frontend RPC function types.
*/
export type GenericOperationRpc = (args: never) => Promise<unknown>;
type ClientOperation<Input, Output> = [Input] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
type ClientOperation<Input, Output> = IfAny<Input, (args?: any) => Promise<Output>, ClientOperationWithNonAnyInput<Input, Output>>;
type ClientOperationWithNonAnyInput<Input, Output> = [
Input
] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
export {};

View File

@ -1,4 +1,4 @@
import { _Awaited, _ReturnType } from '../../universal/types';
import { IfAny, _Awaited, _ReturnType } from '../../universal/types';
import { _Entity, UnauthenticatedOperationDefinition, Payload } from '../_types';
/**
* Constructs the unauthenticated operation's server-side API type from its
@ -24,9 +24,10 @@ export declare function createUnauthenticatedOperation<OperationDefinition exten
* `void` if the operation doesn't expect a payload).
* @template Output The type of the operation's return value.
*/
type UnauthenticatedOperation<Input, Output> = [
type UnauthenticatedOperation<Input, Output> = IfAny<Input, (args?: any) => Promise<Output>, UnauthenticatedOperationWithNonAnyInput<Input, Output>>;
type UnauthenticatedOperationWithNonAnyInput<Input, Output> = [
Input
] extends [never] ? (args: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
/**
* The principal type for an unauthenticated operation's definition (i.e., all
* unauthenticated operation definition types are a subtype of this type).

View File

@ -4,3 +4,4 @@ export type Expand<T> = T extends (...args: infer A) => infer R ? (...args: A) =
export type _Awaited<T> = T extends Promise<infer V> ? _Awaited<V> : T;
export type _ReturnType<T extends (...args: never[]) => unknown> = T extends (...args: never[]) => infer R ? R : never;
export type Tail<T extends [unknown, ...unknown[]]> = T extends [unknown, ...infer R] ? R : never;
export type IfAny<Value, Then, Else> = 0 extends (1 & Value) ? Then : Else;

View File

@ -1,4 +1,4 @@
import { _Awaited, _ReturnType } from '../../universal/types'
import { IfAny, _Awaited, _ReturnType } from '../../universal/types'
import {
_Entity,
@ -54,6 +54,7 @@ export function createUnauthenticatedOperation<
return operation as UnauthenticatedOperationFor<OperationDefinition>
}
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/2170#issue-2398830273
/**
* Constructs the type for an unauthenticated operation's server-side API.
*
@ -62,13 +63,20 @@ export function createUnauthenticatedOperation<
* @template Output The type of the operation's return value.
*/
type UnauthenticatedOperation<Input, Output> =
IfAny<
Input,
(args?: any) => Promise<Output>,
UnauthenticatedOperationWithNonAnyInput<Input, Output>
>
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/1090#discussion_r1159732471
type UnauthenticatedOperationWithNonAnyInput<Input, Output> =
[Input] extends [never]
? (args: unknown) => Promise<Output>
? (args?: unknown) => Promise<Output>
: [Input] extends [void]
? () => Promise<Output>
: (args: Input) => Promise<Output>
/**
* The principal type for an unauthenticated operation's definition (i.e., all
* unauthenticated operation definition types are a subtype of this type).

View File

@ -27,9 +27,13 @@ export type _Awaited<T> = T extends Promise<infer V>
// TODO: investigate how to properly specify the 'extends' constraint for function
// type (i.e., any vs never and unknown) and stick with that. Take DX into
// consideration.
export type _ReturnType<T extends (...args: never[]) => unknown> =
export type _ReturnType<T extends (...args: never[]) => unknown> =
T extends (...args: never[]) => infer R ? R : never
// Returns elements of an array except the first one.
export type Tail<T extends [unknown, ...unknown[]]> = T extends [unknown, ...infer R] ? R : never;
// Source: https://stackoverflow.com/a/55541672
export type IfAny<Value, Then, Else> = 0 extends (1 & Value) ? Then : Else;

View File

@ -102,7 +102,7 @@
"file",
"../out/sdk/wasp/client/operations/rpc.ts"
],
"63fb0670b2ccff4ad005bbb76597572a60e01f31b739d7bc764bb59c4f3c53d4"
"08f30fe4e31a691a225949ccd4d638cffe78bf2f4f2a76feb70133d4d9a5ba94"
],
[
[
@ -305,7 +305,7 @@
"file",
"../out/sdk/wasp/server/operations/wrappers.ts"
],
"4f3ffd00d845664a0122a24d2a12279c0febd744214c7167e7d9dea41330e107"
"8e38950b0929b8fbe662dd796c4c445285f91b37df3d40b83b4685347e5aa3aa"
],
[
[
@ -333,7 +333,7 @@
"file",
"../out/sdk/wasp/universal/types.ts"
],
"791cb40cb5c405ade34d46770a8178353257e8d2351a877b6ca5e88aafb0ec13"
"be0d749bf9ae2f44ca2c281e0fb65c4d30c1d07e375ba38407e6c4fa1f05cc87"
],
[
[

View File

@ -1,5 +1,6 @@
import { type Route } from "wasp/client";
import type {
IfAny,
_Awaited,
_ReturnType,
} from "wasp/universal/types"
@ -55,11 +56,11 @@ export type QueryMetadata = {
*/
export type OperationRpcFor<BackendOperation extends GenericBackendOperation> =
Parameters<BackendOperation> extends []
? ClientOperation<void, _Awaited<_ReturnType<BackendOperation>>>
: ClientOperation<
Parameters<BackendOperation>[0],
_Awaited<_ReturnType<BackendOperation>>
>
? ClientOperation<void, _Awaited<_ReturnType<BackendOperation>>>
: ClientOperation<
Parameters<BackendOperation>[0],
_Awaited<_ReturnType<BackendOperation>>
>
// PRIVATE API (needed in SDK)
/**
@ -74,8 +75,21 @@ export type GenericBackendOperation = (args: never, context: any) => unknown
*/
export type GenericOperationRpc = (args: never) => Promise<unknown>
// NOTE: There's some duplication in the below types.
// Read the discussion here to understand why before trying to remove it:
// https://github.com/wasp-lang/wasp/pull/2170#discussion_r1671285049
//
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/2170#issue-2398830273
type ClientOperation<Input, Output> =
IfAny<
Input,
(args?: any) => Promise<Output>,
ClientOperationWithNonAnyInput<Input, Output>
>
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/1090#discussion_r1159732471
type ClientOperation<Input, Output> = [Input] extends [never]
type ClientOperationWithNonAnyInput<Input, Output> =
[Input] extends [never]
? (args?: unknown) => Promise<Output>
: [Input] extends [void]
? () => Promise<Output>

View File

@ -1,5 +1,5 @@
import { type Route } from "wasp/client";
import type { _Awaited, _ReturnType } from "wasp/universal/types";
import type { IfAny, _Awaited, _ReturnType } from "wasp/universal/types";
/**
* The client Query object type. It's a callable Query function with some extra
* properties (metadata).
@ -34,5 +34,8 @@ export type GenericBackendOperation = (args: never, context: any) => unknown;
* A supertype of all possible frontend RPC function types.
*/
export type GenericOperationRpc = (args: never) => Promise<unknown>;
type ClientOperation<Input, Output> = [Input] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
type ClientOperation<Input, Output> = IfAny<Input, (args?: any) => Promise<Output>, ClientOperationWithNonAnyInput<Input, Output>>;
type ClientOperationWithNonAnyInput<Input, Output> = [
Input
] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
export {};

View File

@ -1,4 +1,4 @@
import { _Awaited, _ReturnType } from '../../universal/types';
import { IfAny, _Awaited, _ReturnType } from '../../universal/types';
import { _Entity, UnauthenticatedOperationDefinition, Payload } from '../_types';
/**
* Constructs the unauthenticated operation's server-side API type from its
@ -24,9 +24,10 @@ export declare function createUnauthenticatedOperation<OperationDefinition exten
* `void` if the operation doesn't expect a payload).
* @template Output The type of the operation's return value.
*/
type UnauthenticatedOperation<Input, Output> = [
type UnauthenticatedOperation<Input, Output> = IfAny<Input, (args?: any) => Promise<Output>, UnauthenticatedOperationWithNonAnyInput<Input, Output>>;
type UnauthenticatedOperationWithNonAnyInput<Input, Output> = [
Input
] extends [never] ? (args: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
] extends [never] ? (args?: unknown) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args: Input) => Promise<Output>;
/**
* The principal type for an unauthenticated operation's definition (i.e., all
* unauthenticated operation definition types are a subtype of this type).

View File

@ -4,3 +4,4 @@ export type Expand<T> = T extends (...args: infer A) => infer R ? (...args: A) =
export type _Awaited<T> = T extends Promise<infer V> ? _Awaited<V> : T;
export type _ReturnType<T extends (...args: never[]) => unknown> = T extends (...args: never[]) => infer R ? R : never;
export type Tail<T extends [unknown, ...unknown[]]> = T extends [unknown, ...infer R] ? R : never;
export type IfAny<Value, Then, Else> = 0 extends (1 & Value) ? Then : Else;

View File

@ -1,4 +1,4 @@
import { _Awaited, _ReturnType } from '../../universal/types'
import { IfAny, _Awaited, _ReturnType } from '../../universal/types'
import {
_Entity,
@ -54,6 +54,7 @@ export function createUnauthenticatedOperation<
return operation as UnauthenticatedOperationFor<OperationDefinition>
}
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/2170#issue-2398830273
/**
* Constructs the type for an unauthenticated operation's server-side API.
*
@ -62,13 +63,20 @@ export function createUnauthenticatedOperation<
* @template Output The type of the operation's return value.
*/
type UnauthenticatedOperation<Input, Output> =
IfAny<
Input,
(args?: any) => Promise<Output>,
UnauthenticatedOperationWithNonAnyInput<Input, Output>
>
// Read this to understand the type: https://github.com/wasp-lang/wasp/pull/1090#discussion_r1159732471
type UnauthenticatedOperationWithNonAnyInput<Input, Output> =
[Input] extends [never]
? (args: unknown) => Promise<Output>
? (args?: unknown) => Promise<Output>
: [Input] extends [void]
? () => Promise<Output>
: (args: Input) => Promise<Output>
/**
* The principal type for an unauthenticated operation's definition (i.e., all
* unauthenticated operation definition types are a subtype of this type).

View File

@ -27,9 +27,13 @@ export type _Awaited<T> = T extends Promise<infer V>
// TODO: investigate how to properly specify the 'extends' constraint for function
// type (i.e., any vs never and unknown) and stick with that. Take DX into
// consideration.
export type _ReturnType<T extends (...args: never[]) => unknown> =
export type _ReturnType<T extends (...args: never[]) => unknown> =
T extends (...args: never[]) => infer R ? R : never
// Returns elements of an array except the first one.
export type Tail<T extends [unknown, ...unknown[]]> = T extends [unknown, ...infer R] ? R : never;
// Source: https://stackoverflow.com/a/55541672
export type IfAny<Value, Then, Else> = 0 extends (1 & Value) ? Then : Else;

View File

@ -220,6 +220,7 @@ job mySpecialScheduledJob {
}
// --------- Testing --------- //
action testingAction {
fn: import { testingAction } from "@src/testTypes/operations/server",
entities: []
@ -229,8 +230,16 @@ query getDate {
fn: import { getDate } from "@src/testTypes/operations/definitions"
}
query getAnything {
fn: import { getAnything } from "@src/testTypes/operations/definitions",
query getAnythingNoAuth {
fn: import { getAnythingNoAuth } from "@src/testTypes/operations/definitions",
auth: false,
entities: []
}
query getAnythingAuth {
fn: import { getAnythingAuth } from "@src/testTypes/operations/definitions",
auth: true,
entities: []
}
@ -239,6 +248,25 @@ query getTrueVoid {
entities: []
}
query getAnyNoAuth {
fn: import { getAnyNoAuth } from "@src/testTypes/operations/definitions",
auth: false,
entities: []
}
query getAnyAuth {
fn: import { getAnyAuth } from "@src/testTypes/operations/definitions",
auth: true,
entities: []
}
query getAnyToNumberSpecified {
fn: import { getAnyToNumberSpecified } from "@src/testTypes/operations/definitions",
auth: true,
entities: []
}
action taskToTaskUnspecified {
fn: import { taskToTaskUnspecified } from "@src/testTypes/operations/definitions",
entities: [Task]

View File

@ -12,7 +12,7 @@ import {
getTasks,
getTask,
getDate,
getAnything,
getAnythingAuth,
} from 'wasp/client/operations'
import React, { useState, FormEventHandler, ChangeEventHandler } from 'react'
@ -259,7 +259,7 @@ async function logAll() {
const date = await getDate()
console.info('Got date:', date)
const anything = await getAnything()
const anything = await getAnythingAuth()
console.info('Got anything:', anything)
}

View File

@ -2,7 +2,7 @@ import { AuthUser } from 'wasp/auth'
import { getMe } from 'wasp/client/auth'
import {
getDate,
getAnything,
getAnythingAuth,
getTrueVoid,
voidToStringAuth,
voidToStringNoAuth,
@ -14,6 +14,10 @@ import {
taskToTaskUnspecified,
taskToTaskSatisfies,
taskToTaskSpecified,
getAnyAuth,
getAnyNoAuth,
getAnyToNumberSpecified,
getAnythingNoAuth,
} from 'wasp/client/operations'
import {
@ -54,10 +58,19 @@ type TestCases = [
Expect<Equal<typeof getDate, QueryMetadata & (() => Promise<Date>)>>,
Expect<
Equal<
typeof getAnything,
typeof getAnythingAuth,
QueryMetadata & ((args?: unknown) => Promise<Payload>)
>
>,
Expect<
Equal<
typeof getAnythingNoAuth,
QueryMetadata & ((args?: unknown) => Promise<Payload>)
>
>,
Expect<Equal<typeof getTrueVoid, QueryMetadata & (() => Promise<string>)>>,
Expect<Equal<typeof getMe, QueryMetadata & (() => Promise<AuthUser | null>)>>
Expect<Equal<typeof getAnyNoAuth, QueryMetadata & ((args?: any) => Promise<any>)>>,
Expect<Equal<typeof getAnyAuth, QueryMetadata & ((args?: any) => Promise<any>)>>,
Expect<Equal<typeof getAnyToNumberSpecified, QueryMetadata & ((args?: any) => Promise<number>)>>,
Expect<Equal<typeof getMe, QueryMetadata & (() => Promise<AuthUser | null>)>>,
]

View File

@ -7,8 +7,10 @@ import type {
TaskToTaskSatisfies,
TaskToTaskSpecified,
GetDate,
GetAnything,
GetAnythingAuth,
GetAnythingNoAuth,
GetTrueVoid,
GetAnyToNumberSpecified,
} from 'wasp/server/operations'
export const taskToTaskUnspecified = async (args: Task) => args
@ -47,7 +49,7 @@ export const voidToStringNoAuth: VoidToStringNoAuth<void, string> = async (
return 'void'
}
export const unspecifiedToNumber = (async (args) => {
export const unspecifiedToNumber = (async (_args) => {
return 10
}) satisfies UnspecifiedToNumber
@ -101,10 +103,27 @@ export const getDate: GetDate<void, Date> = async () => {
return new Date()
}
export const getAnything: GetAnything = async () => {
export const getAnythingAuth: GetAnythingAuth = async () => {
return 'anything'
}
export const getAnythingNoAuth: GetAnythingNoAuth = async () => {
return 'anything'
}
export const getTrueVoid = (async () => {
return 'anything'
}) satisfies GetTrueVoid
export const getAnyNoAuth = (_args: any, _context: any): any => {
return 'anything'
}
export const getAnyAuth = (_args: any, _context: any): any => {
return 'anything'
}
export const getAnyToNumberSpecified: GetAnyToNumberSpecified<any, number> = (_args, _context) => {
return 10
}

View File

@ -9,7 +9,13 @@ import {
taskToTaskUnspecified,
taskToTaskSatisfies,
taskToTaskSpecified,
getAnythingAuth,
getAnythingNoAuth,
getTrueVoid,
getAnyNoAuth,
type TestingAction,
getAnyAuth,
getAnyToNumberSpecified,
} from 'wasp/server/operations'
import {
@ -20,9 +26,10 @@ import {
import { Equal, Expect } from '../helpers'
import { AuthUser } from 'wasp/auth'
import { Task } from 'wasp/entities'
import { Payload } from 'wasp/server/_types'
export const testingAction: TestingAction = async (args, context) => {
// todo(filip): When sorting out the tests, we should also test whether the
export const testingAction: TestingAction = async (_args, context) => {
// TODO: (Filip) When sorting out the tests, we should also test whether the
// outputs are correct. See:
// - https://github.com/wasp-lang/wasp/issues/2024
// - https://github.com/wasp-lang/wasp/issues/2011
@ -42,7 +49,7 @@ export const testingAction: TestingAction = async (args, context) => {
}
type TestCases = [
// todo(filip): Prisma errors casuing this test to fail, try to add Except
// TODO: (FILIP) Prisma errors casuing this test to fail, try to add Except
// after updating Prisma: https://github.com/wasp-lang/wasp/issues/2099
Equal<
typeof taskToTaskUnspecified,
@ -51,7 +58,7 @@ type TestCases = [
ctx: { user: AuthUser }
) => ReturnType<typeof taskToTaskUnspecifiedDefinition>
>,
// todo(filip): Prisma errors casuing this test to fail, try to add Except
// TODO: (FILIP) Prisma errors casuing this test to fail, try to add Except
// after updating Prisma: https://github.com/wasp-lang/wasp/issues/2099
Equal<
typeof taskToTaskSatisfies,
@ -91,5 +98,21 @@ type TestCases = [
typeof boolToVoidAuth,
(payload: boolean, ctx: { user: AuthUser }) => Promise<void>
>
>
>,
Expect<
Equal<
typeof getAnythingAuth,
(args: unknown, ctx: { user: AuthUser }) => Promise<Payload>
>
>,
Expect<
Equal<
typeof getAnythingNoAuth,
(args?: unknown) => Promise<Payload>
>
>,
Expect<Equal<typeof getTrueVoid, (ctx: { user: AuthUser }) => Promise<string>>>,
Expect<Equal<typeof getAnyNoAuth, (args?: any) => Promise<any>>>,
Expect<Equal<typeof getAnyAuth, (args: any, ctx: { user: AuthUser }) => Promise<any>>>,
Expect<Equal<typeof getAnyToNumberSpecified, (args: any, ctx: { user: AuthUser }) => Promise<number>>>,
]