framework-agnostic RPC API with a smart auto-generated TypeScript client
Project description
Welcome to synclane
synclane is a framework-agnostic RPC API with a smart auto-generated
TypeScript client.
Idea
The below must be enough to define an API:
class UserParams(pydantic.BaseModel):
uid: str
class GetUsers(AbstractProcedure):
def call(self, in_: UserParams, context) -> List[UserDetails]:
...
and use an automatically generated frontend TypeScript client:
import { callGetUsers } from "./src/out";
expect(callGetUsers(userParams).$promise).resolves.toEqual(listOfUserDetails);
Benefits
Automated typescript client generation
Of course, it's possible to annotate your API, export an OpenAPI schema and generate a typescript client from it. However it will lack the below nice bits.
Browser Dates done right
Javascript doesn't have a separate date type, so it uses Date for both
python's date and datetime.
Hence when you pass 2000-01-01 to a browser in New York, the browser will
read it as UTC datetime and then convert it to the local timezone, so it will
give you Dec 31, 1991 7:00PM, which is fine if you wanted to work with a
particular moment in time, but what if you wanted to display someone's date of
birth? That's why lacking date type is a problem.
synclane will see that you wanted to pass python's date to the browser and
will automatically prepare it in the browser, so that Jan 1st is preserved in
the case above.
Browser friendly types only
synclane raises an exception if you use types, which browser won't be able to
understand.
No need to define URLs
Once you name a procedure, e.g. AddUser, you just get callAddUser function
in the typescript client. You don't need to define any other identifier like
API endpoint url.
Enums
If your procedure in/out types include enums, they will become available in the typescript client.
Installation
pip install synclane
pydantic is the only dependency.
Usage
- define procedures
- define RPC instance, its error handling method, register procedures and dump TypeScript client code
- connect RPC to an API
- on TypeScript side: import
rpcConfigand initialize:rpcConfig.url: url where RPC is listeningrpcConfig.initFetch(optional): function, which accepts and can mutate fetch options as needed
Example
Step 1: Define procedures
--8<-- "tests/int_tst/main.py:def_procedures"
Step 2: Define RPC, dump TS
--8<-- "tests/int_tst/main.py:def_rpc"
Step 3.a: Connect to Django
/// tab | async rpc
--8<-- "tests/int_tst/main.py:django_async"
///
/// tab | sync rpc
--8<-- "tests/int_tst/main.py:django_sync"
///
Step 3.b: Connect to FastAPI
/// tab | async rpc
--8<-- "tests/int_tst/main.py:fastapi_async"
///
/// tab | async rpc
--8<-- "tests/int_tst/main.py:fastapi_sync"
///
Step 4: Use autogenerated TS client
--8<-- "tests/int_tst/tests/client.test.ts:imports"
--8<-- "tests/int_tst/tests/client.test.ts:rpc_config"
--8<-- "tests/int_tst/tests/client.test.ts:get_user"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file synclane-0.6.1.tar.gz.
File metadata
- Download URL: synclane-0.6.1.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3dbb8d4ade27349898745db7d0f1472eea903434dd32b4c7990854a7fafaf7b
|
|
| MD5 |
e77a72360ff50c4d83df4f80c725ae9e
|
|
| BLAKE2b-256 |
530e77f0b766a33192f80a8c04f556bef81cd17d9d4cd43a2150f09e28a97c4d
|
Provenance
The following attestation bundles were made for synclane-0.6.1.tar.gz:
Publisher:
pytest.yml on westandskif/synclane
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
synclane-0.6.1.tar.gz -
Subject digest:
d3dbb8d4ade27349898745db7d0f1472eea903434dd32b4c7990854a7fafaf7b - Sigstore transparency entry: 155449811
- Sigstore integration time:
-
Permalink:
westandskif/synclane@e08f5b143f03ec4b1d76529d32b857ac352481a6 -
Branch / Tag:
refs/tags/v0.6.1 - Owner: https://github.com/westandskif
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pytest.yml@e08f5b143f03ec4b1d76529d32b857ac352481a6 -
Trigger Event:
push
-
Statement type:
File details
Details for the file synclane-0.6.1-py3-none-any.whl.
File metadata
- Download URL: synclane-0.6.1-py3-none-any.whl
- Upload date:
- Size: 11.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17c1998f42c6c89ab8b48afce6bd2db72cbbfb9787e7e662d39cf2bc72cc8827
|
|
| MD5 |
48368ad86f832f8092cb6fab668825b7
|
|
| BLAKE2b-256 |
193b2a8808ed22eb500738639370a79be181e8bf02caf25671f0a7e9baed6b0e
|
Provenance
The following attestation bundles were made for synclane-0.6.1-py3-none-any.whl:
Publisher:
pytest.yml on westandskif/synclane
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
synclane-0.6.1-py3-none-any.whl -
Subject digest:
17c1998f42c6c89ab8b48afce6bd2db72cbbfb9787e7e662d39cf2bc72cc8827 - Sigstore transparency entry: 155449812
- Sigstore integration time:
-
Permalink:
westandskif/synclane@e08f5b143f03ec4b1d76529d32b857ac352481a6 -
Branch / Tag:
refs/tags/v0.6.1 - Owner: https://github.com/westandskif
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pytest.yml@e08f5b143f03ec4b1d76529d32b857ac352481a6 -
Trigger Event:
push
-
Statement type: