0.80.2 • Published 4 months ago

@arrirpc/codegen-ts v0.80.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Arri Typescript Codegen

Setup

1) Add the generator to your arri config

// arri.config.ts
import { defineConfig, generators } from 'arri';

export default defineConfig({
    generators: [
        generators.typescriptClient({
            clientName: 'MyClient',
            outputFile: './client/src/myClient.g.ts',
        }),
    ],
});

Options:

NameDescription
outputFile (required)Path to a file that will be created by the generator
clientNameThe name of the generated client
typePrefixAdd a prefix to the generated type names
rootServiceSet the root service of the generated client
prettierOptionsFormatting options for the generated code
rpcGeneratorsOverride the default function used for creating procedures

2) Install the TS client library

Make sure that the project that will be using the generated code has the client library installed. The version number should match your arri cli version.

# npm
npm i @arrirpc/client

# pnpm
pnpm i --save @arrirpc/client

Using the Generated Code

Initialize the client

// This will match whatever you put in the arri config
import { MyClient } from './myClient.g';

const client = new MyClient({
    // only required field
    baseUrl: 'https://example.com',

    // everything below is optional
    headers: () => {
        return {
            Authorization: '<some-token>',
        };
    },
    onError: (err) => {},
    options: {
        retry: 4,
        retryDelay: 200,
        retryStatusCodes: [400, 403, 500, 501],
        onRequest: (ctx) => {},
        onRequestError: (ctx) => {},
        onResponse: (ctx) => {},
        onResponseError: (ctx) => {},
        signal: undefined, // abortcontroller signal
        timeout: 200,
    },
});

await client.myProcedure({ foo: 'foo' });

// individual procedures can also override the global request options
await client.myProcedure({ foo: 'foo' }, { timeout: 400 });
// be aware that these options are not merged with the global request options so you will have to
// re-specify every hook if you only want to change one thing

The root client will be a class containing all of the services and procedures in a single class. If you only need a particular service. You can also import just that service.

For example if we have a some procedures grouped under "users" we can import just that service like so.

import { MyClientUsersService } from './myClient.g';

const usersService = new MyClientUsersService({
    baseUrl: 'https://example.com',
    headers: () => {
        return {
            Authorization: '<some-token>',
        };
    },
});

usersService.someProcedure();

Importing and using types

All generated types and serializers/parsers can be imported from the generated code as well. Types will match the ID given to them in the schema. Serialization and parsing helpers will be prefixed with a $$

// import the user type and the user helper
import { type User, $$User } from './myClient.g';

const bob: User = {
    // field
};

$$User.serialize(bob); // outputs valid JSON
$$User.parse('<some-json-string>'); // outputs a User

Development

Building

Run nx build ts-codegen to build the library.

Running unit tests

Run nx test ts-codegen to execute the unit tests via Vitest.

0.64.0

11 months ago

0.78.0

7 months ago

0.76.4

7 months ago

0.76.3

7 months ago

0.74.0

9 months ago

0.76.0

8 months ago

0.74.1

9 months ago

0.72.0

9 months ago

0.70.1

10 months ago

0.69.2

11 months ago

0.69.1

11 months ago

0.67.0

11 months ago

0.65.2

11 months ago

0.65.1

11 months ago

0.63.3

11 months ago

0.69.0

11 months ago

0.76.2-beta.0

7 months ago

0.76.3-beta.0

7 months ago

0.65.0

11 months ago

0.80.0

4 months ago

0.80.1

4 months ago

0.80.2

4 months ago

0.77.1

7 months ago

0.77.0

7 months ago

0.75.2

8 months ago

0.79.1

5 months ago

0.79.0

6 months ago

0.73.0

9 months ago

0.75.1

8 months ago

0.75.0

8 months ago

0.71.1

9 months ago

0.71.0

9 months ago

0.66.0

11 months ago

0.68.0

11 months ago

0.63.2

1 year ago

0.63.0

1 year ago

0.62.0

1 year ago

0.63.1

1 year ago

0.61.1

1 year ago

0.60.3

1 year ago

0.60.2

1 year ago

0.61.0

1 year ago

0.60.1

1 year ago

0.57.4

1 year ago

0.57.5

1 year ago

0.59.0

1 year ago

0.58.1

1 year ago

0.57.2

1 year ago

0.57.3

1 year ago

0.57.0

1 year ago

0.58.0

1 year ago

0.55.0

1 year ago

0.53.2

1 year ago

0.57.8

1 year ago

0.57.6

1 year ago

0.53.0

1 year ago

0.51.2

1 year ago

0.54.0

1 year ago

0.53.1

1 year ago

0.51.0

1 year ago

0.51.1

1 year ago

0.50.0

1 year ago

0.49.1

1 year ago

0.48.2

1 year ago

0.45.6

1 year ago

0.48.0

1 year ago

0.49.0

1 year ago

0.48.1

1 year ago

0.46.0

1 year ago

0.47.0

1 year ago

0.45.7

1 year ago

0.45.5

1 year ago

0.45.4

1 year ago

0.45.3

1 year ago

0.45.2

1 year ago

0.45.1

1 year ago

0.45.0

1 year ago