TypeScript Config SDK Quick Start
Install
npm install @autoblocks/client
Create a config class
You can create a config class using AutoblocksConfig
and specifying the value type.
type RemoteConfigSchema = {
my_val: string;
}
const config = new AutoblocksConfig<RemoteConfigSchema>({
my_val: 'initial-val',
});
// Usage
config.value.my_val
Create a remote config
Go to the configs page and click Create Config to create your first config. The config must be valid JSON.
The code samples below are using this example config:
Activate the remote config
Create a single instance of the config class for the lifetime of your application. The only required argument when initializing a config is the default value.
import { AutoblocksConfig } from '@autoblocks/client/configs';
import { z } from 'zod';
const zRemoteConfigSchema = z.object({
my_val: z.string().min(1),
});
type RemoteConfigSchema = z.infer<typeof zRemoteConfigSchema>;
const config = new AutoblocksConfig<RemoteConfigSchema>({
my_val: 'initial-val',
});
await config.activateFromRemote({
config: {
id: 'my-remote-config',
version: {
major: 1,
latest: true,
}
},
parser: zRemoteConfigSchema.parse,
});
// Usage
config.value.my_val
When the version is set to LATEST
, the config is periodically refreshes the in-memory values
in the background according to the refresh_interval
.
See the AutoblocksConfigManager
reference for more information.
We are using Zod
for schema validation in this example. You can use any schema validation library you prefer.
Develop locally against a remote config revision that hasn't been deployed
As you create new revisions in the UI, your private revisions (or revisions that have been shared by your teammates)
can be pulled down using dangerouslyUseUndeployed
:
import { AutoblocksConfig } from '@autoblocks/client/configs';
import { z } from 'zod';
const zRemoteConfigSchema = z.object({
my_val: z.string().min(1),
});
type RemoteConfigSchema = z.infer<typeof zRemoteConfigSchema>;
const config = new AutoblocksConfig<RemoteConfigSchema>({
my_val: 'initial-val',
});
await config.activateFromRemote({
config: {
id: 'my-remote-config',
dangerouslyUseUndeployed: {
latest: true,
},
},
parser: zRemoteConfigSchema.parse,
});
// Usage
config.value.my_val
As the name suggests, this should only be used in local development and never in production.