Skip to content

Commit

Permalink
feat: beforeRequest hooks to sign transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
rmrt1n committed Aug 16, 2024
1 parent d94f9fc commit ead3990
Show file tree
Hide file tree
Showing 68 changed files with 3,137 additions and 1,034 deletions.
5 changes: 3 additions & 2 deletions .speakeasy/gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ generation:
auth:
oAuth2ClientCredentialsEnabled: true
typescript:
version: 0.2.2
version: 0.8.57
additionalDependencies:
dependencies: {}
dependencies:
viem: ^2.19.1
devDependencies: {}
peerDependencies: {}
additionalPackageJSON: {}
Expand Down
10 changes: 5 additions & 5 deletions .speakeasy/workflow.lock
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
speakeasyVersion: 1.352.0
speakeasyVersion: 1.369.0
sources:
cardinal:
sourceNamespace: cardinal
sourceRevisionDigest: sha256:4cc32242239a8a1d711fafed51ea35a0ac5047e15877ab6fd2e7d3bf41a61716
sourceBlobDigest: sha256:0c2627da1d3124fd952e346bc3c92c28138b31c60eaac80735332a7a90ad9af3
sourceRevisionDigest: sha256:04aa07bfbf0905765d30a7ece2c28ad506466907654e86d1e09c8d219e20ac52
sourceBlobDigest: sha256:c5567d5008ace7b90cc4180957cfdb1f0313f77234d6bbb916588d81de6225d8
tags:
- latest
targets:
cardinal-ts:
source: cardinal
sourceNamespace: cardinal
sourceRevisionDigest: sha256:4cc32242239a8a1d711fafed51ea35a0ac5047e15877ab6fd2e7d3bf41a61716
sourceBlobDigest: sha256:0c2627da1d3124fd952e346bc3c92c28138b31c60eaac80735332a7a90ad9af3
sourceRevisionDigest: sha256:04aa07bfbf0905765d30a7ece2c28ad506466907654e86d1e09c8d219e20ac52
sourceBlobDigest: sha256:c5567d5008ace7b90cc4180957cfdb1f0313f77234d6bbb916588d81de6225d8
outLocation: cardinal/sdk/typescript
workflow:
workflowVersion: 1.0.0
Expand Down
2 changes: 2 additions & 0 deletions cardinal/sdk/typescript/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
/node_modules
/lib
/sdk
/funcs
/hooks
/index.*
/core.*
/cjs
/esm
/dist
Expand Down
37 changes: 28 additions & 9 deletions cardinal/sdk/typescript/.speakeasy/gen.lock
Original file line number Diff line number Diff line change
@@ -1,50 +1,67 @@
lockVersion: 2.0.0
id: fc6bc2ef-bea2-433a-a006-bbd7c75eb7e5
id: 156883ba-7f38-4ec6-b684-ccf304078a11
management:
docChecksum: bca4de5ed616447f5a60b1d34a234055
docChecksum: c21a5ab6f48f4d352f9d4e0c15774eb2
docVersion: 0.0.1
speakeasyVersion: 1.352.0
generationVersion: 2.384.4
releaseVersion: 0.2.2
configChecksum: e54b7a7f6ee784433e085cd30e3d2e69
speakeasyVersion: 1.369.0
generationVersion: 2.399.0
releaseVersion: 0.8.57
configChecksum: fd1eda0d4a220de889417bb09be99563
features:
typescript:
additionalDependencies: 0.1.0
core: 3.11.12
core: 3.13.2
defaultEnabledRetries: 0.1.0
envVarSecurityUsage: 0.1.0
envVarSecurityUsage: 0.1.1
globalSecurityCallbacks: 0.1.0
globalServerURLs: 2.82.4
globals: 2.82.1
hiddenGlobals: 0.1.0
ignores: 2.81.1
nameOverrides: 2.81.2
responseFormat: 0.2.3
retries: 2.83.0
sdkHooks: 0.1.0
generatedFiles:
- src/funcs/queryCql.ts
- src/funcs/getDebugState.ts
- src/funcs/getHealth.ts
- src/funcs/query.ts
- src/funcs/getReceipts.ts
- src/funcs/transact.ts
- src/funcs/createPersona.ts
- src/funcs/getWorld.ts
- src/sdk/sdk.ts
- .eslintrc.cjs
- .npmignore
- FUNCTIONS.md
- RUNTIMES.md
- jsr.json
- package.json
- src/core.ts
- src/index.ts
- src/lib/base64.ts
- src/lib/config.ts
- src/lib/dlv.ts
- src/lib/encodings.ts
- src/lib/http.ts
- src/lib/is-plain-object.ts
- src/lib/logger.ts
- src/lib/matchers.ts
- src/lib/primitives.ts
- src/lib/retries.ts
- src/lib/schemas.ts
- src/lib/sdks.ts
- src/lib/security.ts
- src/lib/url.ts
- src/sdk/index.ts
- src/models/errors/httpclienterrors.ts
- src/models/errors/sdkerror.ts
- src/models/errors/sdkvalidationerror.ts
- src/types/blobs.ts
- src/types/constdatetime.ts
- src/types/enums.ts
- src/types/fp.ts
- src/types/index.ts
- src/types/operations.ts
- src/types/rfcdate.ts
Expand All @@ -63,6 +80,7 @@ generatedFiles:
- src/models/components/pkgworlddevworldenginecardinaltypesfielddetail.ts
- src/models/operations/postquerygamequeryname.ts
- src/models/operations/posttxgametxname.ts
- src/models/operations/posttxpersonacreatepersona.ts
- src/models/errors/index.ts
- src/models/components/index.ts
- src/models/operations/index.ts
Expand All @@ -78,15 +96,16 @@ generatedFiles:
- docs/models/components/cardinalserverhandlerreceiptentry.md
- docs/models/components/cardinalserverhandlerlisttxreceiptsrequest.md
- docs/models/components/cardinalserverhandlerposttransactionresponse.md
- docs/models/components/body.md
- docs/models/components/cardinalserverhandlertransaction.md
- docs/models/components/cardinalserverhandlergetworldresponse.md
- docs/models/components/fields.md
- docs/models/components/pkgworlddevworldenginecardinaltypesfielddetail.md
- docs/models/operations/postquerygamequerynamerequestbody.md
- docs/models/operations/postquerygamequerynamerequest.md
- docs/models/operations/postquerygamequerynameresponsebody.md
- docs/models/operations/posttxgametxnameglobals.md
- docs/models/operations/posttxgametxnamerequest.md
- docs/models/operations/posttxpersonacreatepersonaglobals.md
- docs/lib/utils/retryconfig.md
- docs/sdks/cardinal/README.md
- USAGE.md
Expand Down
102 changes: 102 additions & 0 deletions cardinal/sdk/typescript/FUNCTIONS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Standalone Functions

> [!NOTE]
> This section is useful if you are using a bundler and targetting browsers and
> runtimes where the size of an application affects performance and load times.
Every method in this SDK is also available as a standalone function. This
alternative API is suitable when targetting the browser or serverless runtimes
and using a bundler to build your application since all unused functionality
will be tree-shaken away. This includes code for unused methods, Zod schemas,
encoding helpers and response handlers. The result is dramatically smaller
impact on the application's final bundle size which grows very slowly as you use
more and more functionality from this SDK.

Calling methods through the main SDK class remains a valid and generally more
more ergonomic option. Standalone functions represent an optimisation for a
specific category of applications.

## Example

```typescript
import { CardinalCore } from "cardinal/core.js";
import { queryCql } from "cardinal/funcs/queryCql.js";
import { SDKValidationError } from "cardinal/models/errors/sdkvalidationerror.js";

// Use `CardinalCore` for best tree-shaking performance.
// You can create one instance of it to use across an application.
const cardinal = new CardinalCore();

async function run() {
const res = await queryCql(cardinal, {});

switch (true) {
case res.ok:
// The success case will be handled outside of the switch block
break;
case res.error instanceof SDKValidationError:
// Pretty-print validation errors.
return console.log(res.error.pretty());
case res.error instanceof Error:
return console.log(res.error);
default:
// TypeScript's type checking will fail on the following line if the above
// cases were not exhaustive.
res.error satisfies never;
throw new Error("Assertion failed: expected error checks to be exhaustive: " + res.error);
}


const { value: result } = res;

// Handle the result
console.log(result)
}

run();
```

## Result types

Standalone functions differ from SDK methods in that they return a
`Result<Value, Error>` type to capture _known errors_ and document them using
the type system. By avoiding throwing errors, application code maintains clear
control flow and error-handling become part of the regular flow of application
code.

> We use the term "known errors" because standalone functions, and JavaScript
> code in general, can still throw unexpected errors such as `TypeError`s,
> `RangeError`s and `DOMException`s. Exhaustively catching all errors may be
> something this SDK addresses in the future. Nevertheless, there is still a lot
> of benefit from capturing most errors and turning them into values.
The second reason for this style of programming is because these functions will
typically be used in front-end applications where exception throwing is
sometimes discouraged or considered unidiomatic. React and similar ecosystems
and libraries tend to promote this style of programming so that components
render useful content under all states (loading, success, error and so on).

The general pattern when calling standalone functions looks like this:

```typescript
import { Core } from "<sdk-package-name>";
import { fetchSomething } from "<sdk-package-name>/funcs/fetchSomething.js";

const client = new Core();

async function run() {
const result = await fetchSomething(client, { id: "123" });
if (!result.ok) {
// You can throw the error or handle it. It's your choice now.
throw result.error;
}

console.log(result.value);
}

run();
```

Notably, `result.error` above will have an explicit type compared to a try-catch
variation where the error in the catch block can only be of type `unknown` (or
`any` depending on your TypeScript settings).
83 changes: 83 additions & 0 deletions cardinal/sdk/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,89 @@ const sdk = new Cardinal({ httpClient });
```
<!-- End Custom HTTP Client [http-client] -->

<!-- Start Debugging [debug] -->
## Debugging

To log HTTP requests and responses, you can pass a logger that matches `console`'s interface as an SDK option.

> [!WARNING]
> Beware that debug logging will reveal secrets, like API tokens in headers, in log messages printed to a console or files. It's recommended to use this feature only during local development and not in production.
```typescript
import { Cardinal } from "cardinal";

const sdk = new Cardinal({ debugLogger: console });
```
<!-- End Debugging [debug] -->

<!-- Start Standalone functions [standalone-funcs] -->
## Standalone functions

All the methods listed above are available as standalone functions. These
functions are ideal for use in applications running in the browser, serverless
runtimes or other environments where application bundle size is a primary
concern. When using a bundler to build your application, all unused
functionality will be either excluded from the final bundle or tree-shaken away.

To read more about standalone functions, check [FUNCTIONS.md](./FUNCTIONS.md).

<details>

<summary>Available standalone functions</summary>

- [createPersona](docs/sdks/cardinal/README.md#createpersona)
- [getDebugState](docs/sdks/cardinal/README.md#getdebugstate)
- [getHealth](docs/sdks/cardinal/README.md#gethealth)
- [getReceipts](docs/sdks/cardinal/README.md#getreceipts)
- [getWorld](docs/sdks/cardinal/README.md#getworld)
- [queryCql](docs/sdks/cardinal/README.md#querycql)
- [query](docs/sdks/cardinal/README.md#query)
- [transact](docs/sdks/cardinal/README.md#transact)


</details>
<!-- End Standalone functions [standalone-funcs] -->

<!-- Start Global Parameters [global-parameters] -->
## Global Parameters

Certain parameters are configured globally. These parameters may be set on the SDK client instance itself during initialization. When configured as an option during SDK initialization, These global values will be used as defaults on the operations that use them. When such operations are called, there is a place in each to override the global value, if needed.

For example, you can set `_privateKey` to `"<value>"` at SDK initialization and then you do not have to pass the same value on calls to operations like `transact`. But if you want to do so you may, which will locally override the global setting. See the example code below for a demonstration.


### Available Globals

The following global parameters are available.

| Name | Type | Required | Description |
| ---- | ---- |:--------:| ----------- |
| privateKey | string | | The privateKey parameter. |
| namespace | string | | The namespace parameter. |


### Example

```typescript
import { Cardinal } from "cardinal";

const cardinal = new Cardinal();

async function run() {
const result = await cardinal.transact({
txName: "<value>",
cardinalServerHandlerTransaction: {},
});

// Handle the result
console.log(result);
}

run();

```
<!-- End Global Parameters [global-parameters] -->

<!-- Placeholder for Future Speakeasy SDK Sections -->

# Development
Expand Down
9 changes: 0 additions & 9 deletions cardinal/sdk/typescript/docs/models/components/body.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CardinalServerHandlerCQLQueryRequest

## Example Usage

```typescript
import { CardinalServerHandlerCQLQueryRequest } from "cardinal/models/components";

let value: CardinalServerHandlerCQLQueryRequest = {};
```

## Fields

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CardinalServerHandlerCQLQueryResponse

## Example Usage

```typescript
import { CardinalServerHandlerCQLQueryResponse } from "cardinal/models/components";

let value: CardinalServerHandlerCQLQueryResponse = {};
```

## Fields

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CardinalServerHandlerGetHealthResponse

## Example Usage

```typescript
import { CardinalServerHandlerGetHealthResponse } from "cardinal/models/components";

let value: CardinalServerHandlerGetHealthResponse = {};
```

## Fields

Expand Down
Loading

0 comments on commit ead3990

Please sign in to comment.