forked from analogjs/analog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(trpc): add rxjs observable compatible trpc client (analogjs#385)
This commit introduces a rxjs version of the tRPC client. Instead of returning a Promise, the client now returns observables for all operations. Therefore it behaves similar to the built in Angular HttpClient. closes analogjs#379
- Loading branch information
1 parent
a44939c
commit 9749309
Showing
7 changed files
with
471 additions
and
403 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html lang='en'> | ||
<head> | ||
<meta charset='utf-8' /> | ||
<title>SPARTAN - Notes App</title> | ||
<base href='/' /> | ||
<meta name='viewport' content='width=device-width, initial-scale=1' /> | ||
<link rel="icon" type="image/x-icon" href="/favicon.ico"> | ||
<link rel='stylesheet' href='/src/styles.css' /> | ||
</head> | ||
<body> | ||
<trpc-app-root></trpc-app-root> | ||
<script type='module' src='/src/main.ts'></script> | ||
</body> | ||
<html class="dark h-full" lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>SPARTAN - Notes App</title> | ||
<base href="/" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<link rel="icon" type="image/x-icon" href="/favicon.ico" /> | ||
<link rel="stylesheet" href="/src/styles.css" /> | ||
</head> | ||
<body class="h-full bg-zinc-900"> | ||
<trpc-app-root></trpc-app-root> | ||
<script type="module" src="/src/main.ts"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { | ||
AnyRouter, | ||
ClientDataTransformerOptions, | ||
CombinedDataTransformer, | ||
DataTransformerOptions, | ||
DefaultDataTransformer, | ||
} from '@trpc/server'; | ||
import { | ||
OperationContext, | ||
OperationLink, | ||
OperationResultObservable, | ||
} from '@trpc/client/src/links/types'; | ||
import { Operation, TRPCLink } from '@trpc/client'; | ||
import { observable } from '@trpc/server/observable'; | ||
|
||
// Removed subscription | ||
export type TRPCType = 'query' | 'mutation'; | ||
|
||
// Removed subscription and requestAsPromise | ||
export type UntypedClientProperties = | ||
| 'links' | ||
| 'runtime' | ||
| 'requestId' | ||
| '$request' | ||
| 'query' | ||
| 'mutation'; | ||
|
||
/* | ||
* One to one copy of the trpc client internal code | ||
* Nothing was changed, but we can not import these methods because | ||
* they are not exported | ||
*/ | ||
export type IntersectionError<TKey extends string> = | ||
`The property '${TKey}' in your router collides with a built-in method, rename this router or procedure on your backend.`; | ||
|
||
export interface TRPCRequestOptions { | ||
/** | ||
* Pass additional context to links | ||
*/ | ||
context?: OperationContext; | ||
} | ||
|
||
export function createChain< | ||
TRouter extends AnyRouter, | ||
TInput = unknown, | ||
TOutput = unknown | ||
>(opts: { | ||
links: OperationLink<TRouter, TInput, TOutput>[]; | ||
op: Operation<TInput>; | ||
}): OperationResultObservable<TRouter, TOutput> { | ||
return observable((observer) => { | ||
function execute(index = 0, op = opts.op) { | ||
const next = opts.links[index]; | ||
if (!next) { | ||
throw new Error( | ||
'No more links to execute - did you forget to add an ending link?' | ||
); | ||
} | ||
const subscription = next({ | ||
op, | ||
next(nextOp) { | ||
const nextObserver = execute(index + 1, nextOp); | ||
|
||
return nextObserver; | ||
}, | ||
}); | ||
return subscription; | ||
} | ||
|
||
const obs$ = execute(); | ||
return obs$.subscribe(observer); | ||
}); | ||
} | ||
|
||
export type CreateTRPCClientOptions<TRouter extends AnyRouter> = | ||
| CreateTRPCClientBaseOptions<TRouter> & { | ||
links: TRPCLink<TRouter>[]; | ||
}; | ||
|
||
export type CreateTRPCClientBaseOptions<TRouter extends AnyRouter> = | ||
TRouter['_def']['_config']['transformer'] extends DefaultDataTransformer | ||
? { | ||
/** | ||
* Data transformer | ||
* | ||
* You must use the same transformer on the backend and frontend | ||
* @link https://trpc.io/docs/data-transformers | ||
**/ | ||
transformer?: 'You must set a transformer on the backend router'; | ||
} | ||
: TRouter['_def']['_config']['transformer'] extends DataTransformerOptions | ||
? { | ||
/** | ||
* Data transformer | ||
* | ||
* You must use the same transformer on the backend and frontend | ||
* @link https://trpc.io/docs/data-transformers | ||
**/ | ||
transformer: TRouter['_def']['_config']['transformer'] extends CombinedDataTransformer | ||
? DataTransformerOptions | ||
: TRouter['_def']['_config']['transformer']; | ||
} | ||
: { | ||
/** | ||
* Data transformer | ||
* | ||
* You must use the same transformer on the backend and frontend | ||
* @link https://trpc.io/docs/data-transformers | ||
**/ | ||
transformer?: | ||
| /** @deprecated **/ ClientDataTransformerOptions | ||
| CombinedDataTransformer; | ||
}; |
Oops, something went wrong.