Skip to content

Commit

Permalink
Fix SDK relation type (directus#23877)
Browse files Browse the repository at this point in the history
Co-authored-by: Pascal Jufer <[email protected]>
  • Loading branch information
br41nslug and paescuj authored Oct 18, 2024
1 parent 72ad6bb commit 75080fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-bottles-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@directus/sdk": patch
---

Improved SDK relation function type
24 changes: 6 additions & 18 deletions docs/reference/system/relations.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ import { createDirectus, rest, readRelations } from '@directus/sdk';

const client = createDirectus('directus_project_url').with(rest());

const result = await client.request(readRelations(query_object));
const result = await client.request(readRelations());
```

</template>
Expand Down Expand Up @@ -189,11 +189,7 @@ import { createDirectus, rest, readRelations } from '@directus/sdk';

const client = createDirectus('https://directus.example.com').with(rest());

const result = await client.request(
readRelations({
fields: ['*'],
})
);
const result = await client.request(readRelations());
```

</template>
Expand Down Expand Up @@ -236,7 +232,7 @@ import { createDirectus, rest, readRelationByCollection } from '@directus/sdk';

const client = createDirectus('directus_project_url').with(rest());

const result = await client.request(readRelationByCollection(collection_name, query_object));
const result = await client.request(readRelationByCollection(collection_name));
```

</template>
Expand Down Expand Up @@ -279,11 +275,7 @@ import { createDirectus, rest, readRelationByCollection } from '@directus/sdk';

const client = createDirectus('https://directus.example.com').with(rest());

const result = await client.request(
readRelationByCollection('articles', {
fields: ['*'],
})
);
const result = await client.request(readRelationByCollection('articles'));
```

</template>
Expand Down Expand Up @@ -319,7 +311,7 @@ import { createDirectus, rest, readRelation } from '@directus/sdk';

const client = createDirectus('directus_project_url').with(rest());

const result = await client.request(readRelation(collection_name, field_name, query_object));
const result = await client.request(readRelation(collection_name, field_name));
```

</template>
Expand Down Expand Up @@ -363,11 +355,7 @@ import { createDirectus, rest, readRelation } from '@directus/sdk';

const client = createDirectus('https://directus.example.com').with(rest());

const result = await client.request(
readRelation('articles', 'authors', {
fields: ['*'],
})
);
const result = await client.request(readRelation('articles', 'authors'));
```

</template>
Expand Down
26 changes: 10 additions & 16 deletions sdk/src/rest/commands/read/relations.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import type { DirectusRelation } from '../../../schema/relation.js';
import type { ApplyQueryFields, Query } from '../../../types/index.js';
import type { ApplyQueryFields } from '../../../types/index.js';
import { throwIfEmpty } from '../../utils/index.js';
import type { RestCommand } from '../../types.js';

export type ReadRelationOutput<Schema, Item extends object = DirectusRelation<Schema>> = ApplyQueryFields<
Schema,
Item,
'*'
>;
export type ReadRelationOutput<Schema> = ApplyQueryFields<Schema, DirectusRelation<Schema>, '*'>;

/**
* List all Relations that exist in Directus.
* @param query The query parameters
* @returns An array of up to limit Relation objects. If no items are available, data will be an empty array.
* @returns An array of Relation objects. If no items are available, data will be an empty array.
*/
export const readRelations =
<Schema>(): RestCommand<ReadRelationOutput<Schema>[], Schema> =>
Expand All @@ -22,31 +17,30 @@ export const readRelations =
});

/**
* List an existing Relation by primary key.
* List all Relations of a collection.
* @param collection The collection
* @returns Returns a Relation object if a valid primary key was provided.
* @throws Will throw if collection is empty
* @returns Returns an array of Relation objects if a valid collection name was provided.
*/
export const readRelationByCollection =
<Schema>(collection: DirectusRelation<Schema>['collection']): RestCommand<ReadRelationOutput<Schema>, Schema> =>
<Schema>(collection: DirectusRelation<Schema>['collection']): RestCommand<ReadRelationOutput<Schema>[], Schema> =>
() => ({
path: `/relations/${collection}`,
method: 'GET',
});

/**
* List an existing Relation by primary key.
* List an existing Relation by collection and field name.
* @param collection The collection
* @param field The field
* @returns Returns a Relation object if a valid primary key was provided.
* @returns Returns a Relation object if a valid collection and field name was provided.
* @throws Will throw if collection is empty
* @throws Will throw if field is empty
*/
export const readRelation =
<Schema, const TQuery extends Query<Schema, DirectusRelation<Schema>>>(
<Schema>(
collection: DirectusRelation<Schema>['collection'],
field: DirectusRelation<Schema>['field'],
): RestCommand<ReadRelationOutput<Schema, TQuery>, Schema> =>
): RestCommand<ReadRelationOutput<Schema>, Schema> =>
() => {
throwIfEmpty(collection, 'Collection cannot be empty');
throwIfEmpty(field, 'Field cannot be empty');
Expand Down

0 comments on commit 75080fa

Please sign in to comment.