@fern-api/merge-snippets
v0.0.1
Published
Generate usage snippets for Merge SDKs
Keywords
Readme
Merge Snippets SDK
The Merge Snippets SDK produces valid usage snippets from arbitrary request data.
Usage
The SDK can be used as follows:
import { SnippetResolver } from "@fern-api/merge-snippets";
const resolver = new SnippetResolver();
const go = resolver.sdk("go");
const candidates = go.endpoint("GET /ats/v1/candidates");
const response = await candidates.generate({
auth: {
type: "bearer",
token: "<YOUR_API_KEY>"
},
queryParameters: {
cursor: "4935a656-a929-4792-b97c-8848be85c27c"
}
});The generated response.snippet will be set to the following:
package example
import (
client "github.com/merge-api/merge-go-client/client"
option "github.com/merge-api/merge-go-client/option"
context "context"
ats "github.com/merge-api/merge-go-client/ats"
merge "github.com/merge-api/merge-go-client"
)
func do() () {
client := client.NewClient(
option.WithApiKey(
"<YOUR_API_KEY>",
),
)
client.Ats.Candidates.List(
context.TODO(),
&ats.CandidatesListRequest{
Cursor: merge.String(
"4935a656-a929-4792-b97c-8848be85c27c",
),
},
)
}Default snippets
You can call the generate method without a request payload to retrieve the default example
of the endpoint like so:
import { SnippetResolver } from "@fern-api/merge-snippets";
const resolver = new SnippetResolver();
const go = resolver.sdk("go");
const candidates = go.endpoint("GET /ats/v1/candidates");
const response = await candidates.generate();Note that the result will omit optional properties, so if a request is entirely composed of optional values, you're better off providing your own request payload.
Sync Methods
The SDK also exposes a generateSync method for users that don't have access to async and await in specific contexts.
It uses the same parameters as generate and can be invoked like so:
import { SnippetResolver } from "@fern-api/merge-snippets";
const resolver = new SnippetResolver();
const go = resolver.sdk("go");
const candidates = go.endpoint("GET /ats/v1/candidates")
const response = candidates.generateSync({ ... });Endpoint Filtering
For performance, the endpoint method filters the provided API specification to only contain the
selected endpoint (e.g. GET /ats/v1/candidates). This means you can reuse the filtered result with a
simple variable for the endpoint like so:
// The 'candidates' variable acts upon an API specification that only contains the 'GET /ats/v1/candidates' endpoint.
const candidates = go.endpoint("POST /ats/v1/candidates")
// Generate multiple snippets using the same instance.
const one = candidates.generateSync({ ... });
const two = candidates.generateSync({ ... });
...