Skip to content

Commit

Permalink
Make operations work with any (#2170)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodic committed Jul 11, 2024
1 parent 3dcaf6b commit 1aa139e
Show file tree
Hide file tree
Showing 50 changed files with 452 additions and 132 deletions.
26 changes: 20 additions & 6 deletions waspc/data/Generator/templates/sdk/wasp/client/operations/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type Route } from "wasp/client";
import type {
IfAny,
_Awaited,
_ReturnType,
} from "wasp/universal/types"
Expand Down Expand Up @@ -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)
/**
Expand All @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{={= =}=}}
import { _Awaited, _ReturnType } from '../../universal/types'
import { IfAny, _Awaited, _ReturnType } from '../../universal/types'

{=# isAuthEnabled =}
import { type AuthUser } from 'wasp/auth'
Expand Down Expand Up @@ -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.
*
Expand All @@ -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]
Expand All @@ -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.
*
Expand All @@ -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).
Expand Down
6 changes: 5 additions & 1 deletion waspc/data/Generator/templates/sdk/wasp/universal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1aa139e

Please sign in to comment.