-
Notifications
You must be signed in to change notification settings - Fork 85
docs: Splitting endpoint page into three use cases #464
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
Open
Swiftaxe
wants to merge
2
commits into
main
Choose a base branch
from
feat/improve-endpoints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
docs/06-concepts/01-working-with-endpoints/01-working-with-endpoints.md
This file contains hidden or 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,131 @@ | ||
| --- | ||
| slug: /concepts/working-with-endpoints | ||
| --- | ||
|
|
||
| # Working with endpoints | ||
|
|
||
| Endpoints define the server methods that the client can call. With Serverpod, you add methods to your endpoint, and your client code will be generated to make the method call. | ||
|
|
||
| For the client code to be generated: | ||
|
|
||
| - Place the endpoint file anywhere under the `lib` directory of your server. | ||
| - Create a class that extends `Endpoint`. | ||
| - Define methods that return a typed `Future` and take a `Session` object as their first argument. | ||
|
|
||
| The `Session` object holds information about the call being made and provides access to the database. | ||
|
|
||
| ## Creating an endpoint | ||
|
|
||
| ```dart | ||
| import 'package:serverpod/serverpod.dart'; | ||
|
|
||
| class ExampleEndpoint extends Endpoint { | ||
| Future<String> hello(Session session, String name) async { | ||
| return 'Hello $name'; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The above code will create an endpoint called `example` (the Endpoint suffix will be removed) with the single `hello` method. To generate the client-side code run `serverpod generate` in the home directory of the server. | ||
|
|
||
| :::info | ||
|
|
||
| You can pass the `--watch` flag to `serverpod generate` to watch for changed files and generate code whenever your source files are updated. This is useful during the development of your server. | ||
|
|
||
| ::: | ||
|
|
||
| ## Calling an endpoint | ||
|
|
||
| On the client side, you can now call the method by calling: | ||
|
|
||
| ```dart | ||
| var result = await client.example.hello('World'); | ||
| ``` | ||
|
|
||
| The client is initialized like this: | ||
|
|
||
| ```dart | ||
| // Sets up a singleton client object that can be used to talk to the server from | ||
| // anywhere in our app. The client is generated from your server code. | ||
| // The client is set up to connect to a Serverpod running on a local server on | ||
| // the default port. You will need to modify this to connect to staging or | ||
| // production servers. | ||
| var client = Client('http://$localhost:8080/') | ||
| ..connectivityMonitor = FlutterConnectivityMonitor(); | ||
| ``` | ||
|
|
||
| If you run the app in an Android emulator, the `localhost` parameter points to `10.0.2.2`, rather than `127.0.0.1` as this is the IP address of the host machine. To access the server from a different device on the same network (such as a physical phone) replace `localhost` with the local ip address. You can find the local ip by running `ifconfig` (Linux/MacOS) or `ipconfig` (Windows). | ||
|
|
||
| Make sure to also update the `publicHost` in the development config to make sure the server always serves the client with the correct path to assets etc. | ||
|
|
||
| ```yaml | ||
| # your_project_server/config/development.yaml | ||
|
|
||
| apiServer: | ||
| port: 8080 | ||
| publicHost: localhost # Change this line | ||
| publicPort: 8080 | ||
| publicScheme: http | ||
| ``` | ||
|
|
||
| ## Passing parameters | ||
|
|
||
| There are some limitations to how endpoint methods can be implemented. Parameters and return types can be of type `bool`, `int`, `double`, `String`, `UuidValue`, `Duration`, `DateTime`, `ByteData`, `Uri`, `BigInt`, or generated serializable objects (see next section). A typed `Future` should always be returned. Null safety is supported. When passing a `DateTime` it is always converted to UTC. | ||
|
|
||
| You can also pass `List`, `Map`, `Record`, and `Set` as parameters, but they need to be strictly typed with one of the types mentioned above. | ||
|
|
||
| :::warning | ||
|
|
||
| While it's possible to pass binary data through a method call and `ByteData`, it is not the most efficient way to transfer large files. See our [file upload](./file-uploads) interface. The size of a call is by default limited to 512 kB. It's possible to change by adding the `maxRequestSize` to your config files. E.g., this will double the request size to 1 MB: | ||
|
|
||
| ```yaml | ||
| maxRequestSize: 1048576 | ||
| ``` | ||
|
|
||
| ::: | ||
|
|
||
| ## Return types | ||
|
|
||
| The return type must be a typed Future. Supported return types are the same as for parameters. | ||
|
|
||
| ## Excluding endpoints from code generation | ||
|
|
||
| If you want the code generator to ignore an endpoint definition, you can annotate either the entire class or individual methods with `@doNotGenerate`. This can be useful if you want to keep the definition in your codebase without generating server or client bindings for it. | ||
|
|
||
| ### Exclude an entire `Endpoint` class | ||
|
|
||
| Annotate the class with `@doNotGenerate` to exclude it entirely: | ||
|
|
||
| ```dart | ||
| import 'package:serverpod/serverpod.dart'; | ||
|
|
||
| @doNotGenerate | ||
| class ExampleEndpoint extends Endpoint { | ||
| Future<String> hello(Session session, String name) async { | ||
| return 'Hello $name'; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The above code will not generate any server or client bindings for the example endpoint. | ||
|
|
||
| ### Exclude individual `Endpoint` methods | ||
|
|
||
| Alternatively, you can exclude individual methods by annotating them with `@doNotGenerate`. | ||
|
|
||
| ```dart | ||
| import 'package:serverpod/serverpod.dart'; | ||
|
|
||
| class ExampleEndpoint extends Endpoint { | ||
| Future<String> hello(Session session, String name) async { | ||
| return 'Hello $name'; | ||
| } | ||
|
|
||
| @doNotGenerate | ||
| Future<String> goodbye(Session session, String name) async { | ||
| return 'Bye $name'; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| In this case the `ExampleEndpoint` will only expose the `hello` method, whereas the `goodbye` method will not be accessible externally. | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.