Skip to content

Commit

Permalink
feat(cli): add config for directus rest client
Browse files Browse the repository at this point in the history
  • Loading branch information
Khartir committed Jul 10, 2024
1 parent 46ab280 commit 669eb93
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ module.exports = {
directusPassword: 'my-directus-password', // ignored if directusToken is provided
directusConfig: {
clientOptions: {}, // see https://docs.directus.io/guides/sdk/getting-started.html#polyfilling
restConfig: {}, // see https://docs.directus.io/packages/@directus/sdk/rest/interfaces/RestConfig.html
}
dumpPath: './directus-config',
collectionsPath: 'collections',
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/lib/services/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,17 @@ export class ConfigService {
@Cacheable()
getDirectusConfig(): DirectusConfigWithToken | DirectusConfigWithCredentials {
const url = this.requireOptions('directusUrl');
const clientConfig = this.getOptions('directusConfig')?.clientOptions;
const directusConfig = this.getOptions('directusConfig');
const clientConfig = directusConfig?.clientOptions;
const restConfig = directusConfig?.restConfig;
const token = this.getOptions('directusToken');
if (token) {
return { url, token, clientConfig };
return { url, token, clientConfig, restConfig };
}

const email = this.requireOptions('directusEmail');
const password = this.requireOptions('directusPassword');
return { url, email, password, clientConfig };
return { url, email, password, clientConfig, restConfig };
}

@Cacheable()
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/lib/services/config/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { z } from 'zod';
import type {
import {
ConfigFileOptionsSchema,
OptionsFields,
OptionsSchema,
CollectionEnum,
CollectionPreservableIdEnum,
ClientConfigSchema,
RestConfigSchema,
} from './schema';
import type { MigrationClient } from '../migration-client';
import type { DirectusBaseType, Query } from '../collections';
Expand Down Expand Up @@ -57,6 +58,7 @@ export interface SnapshotHooks {
interface DirectusConfigBase {
url: string;
clientConfig: z.infer<typeof ClientConfigSchema> | undefined;
restConfig: z.infer<typeof RestConfigSchema> | undefined;
}
export interface DirectusConfigWithToken extends DirectusConfigBase {
token: string;
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/lib/services/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ export const ClientConfigSchema = z.object({
.optional(),
});

export const RestConfigSchema = z.object({
credentials: z.string().optional(),
onRequest: z.function().optional(),
onResponse: z.function().optional(),
});

export const OptionsFields = {
// Global
configPath: z.string().optional(),
Expand All @@ -84,6 +90,7 @@ export const OptionsFields = {
directusConfig: z
.object({
clientOptions: ClientConfigSchema.optional(),
restConfig: RestConfigSchema.optional(),
})
.optional(),
// Pull, diff, push
Expand Down Expand Up @@ -127,6 +134,7 @@ export const ConfigFileOptionsSchema = z.object({
directusConfig: z
.object({
clientOptions: ClientConfigSchema.optional(),
restConfig: RestConfigSchema.optional(),
})
.optional(),
// Dump config
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/lib/services/migration-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
readMe,
rest,
RestClient,
RestConfig,
} from '@directus/sdk';
import { Inject, Service } from 'typedi';
import pino from 'pino';
Expand Down Expand Up @@ -72,7 +73,7 @@ export class MigrationClient {
config.url,
config.clientConfig as ClientOptions | undefined,
)
.with(rest())
.with(rest(config.restConfig as RestConfig | undefined))
.with(authentication());

// If the token is already set, return it
Expand Down

0 comments on commit 669eb93

Please sign in to comment.