Skip to content

Commit 849aab1

Browse files
committed
Add Read more URL to Role.updateCurrentEnvironmentPermissions and tests
Add JSDoc Read more URL to updateCurrentEnvironmentPermissions so it gets grouped under /role/update by extractResourcesEndpointMethods. Add tests for extractAllMethodNames on roles and extractResourcesEndpointMethods.
1 parent 73f0809 commit 849aab1

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

packages/cma-client-analysis/__tests__/code-analysis.test.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import assert from 'node:assert';
2+
import type { ResourcesEndpoint } from '@datocms/rest-api-reference';
23
import {
34
type CmaClientProgram,
45
extractAllMethodNames,
56
extractMethodSignature,
7+
extractResourcesEndpointMethods,
68
extractTypeDependencies,
79
getCmaClientProgram,
810
} from '../src';
@@ -51,6 +53,19 @@ describe('TypeScript Compiler API - Unit Tests', () => {
5153
expect(methodNames).toContain('updateFromUrl');
5254
}, 10000);
5355

56+
it('should extract all method names from roles resource including special methods', () => {
57+
const { checker, clientClass } = cma;
58+
59+
const methodNames = extractAllMethodNames(checker, clientClass, 'roles');
60+
61+
expect(methodNames).toBeDefined();
62+
expect(methodNames.length).toBeGreaterThan(0);
63+
expect(methodNames).toContain('create');
64+
expect(methodNames).toContain('update');
65+
expect(methodNames).toContain('duplicate');
66+
expect(methodNames).toContain('updateCurrentEnvironmentPermissions');
67+
}, 10000);
68+
5469
it('should return empty array for invalid resource', () => {
5570
const { checker, clientClass } = cma;
5671

@@ -199,6 +214,89 @@ describe('TypeScript Compiler API - Unit Tests', () => {
199214
}, 10000);
200215
});
201216

217+
describe('extractResourcesEndpointMethods', () => {
218+
// Minimal endpoint shape: extractResourcesEndpointMethods only reads
219+
// `namespace` and `docUrl`. Cast through unknown to avoid spelling out
220+
// every required field on ResourcesEndpoint.
221+
const makeEndpoint = (
222+
namespace: string,
223+
docUrl: string,
224+
): ResourcesEndpoint => ({ namespace, docUrl }) as unknown as ResourcesEndpoint;
225+
226+
it('groups items.list/rawList/listPagedIterator/rawListPagedIterator under /item/instances', () => {
227+
const { checker, clientClass } = cma;
228+
229+
const methods = extractResourcesEndpointMethods(
230+
checker,
231+
clientClass,
232+
makeEndpoint(
233+
'items',
234+
'https://www.datocms.com/docs/content-management-api/resources/item/instances',
235+
),
236+
);
237+
238+
const names = methods.map((m) => m.name).sort();
239+
expect(names).toEqual(
240+
[
241+
'list',
242+
'listPagedIterator',
243+
'rawList',
244+
'rawListPagedIterator',
245+
].sort(),
246+
);
247+
248+
for (const m of methods) {
249+
expect(m.functionDefinition).toContain(m.name);
250+
expect(m.referencedTypes.size).toBeGreaterThan(0);
251+
}
252+
}, 10000);
253+
254+
it('groups roles.update and rawUpdate under /role/update', () => {
255+
const { checker, clientClass } = cma;
256+
257+
const methods = extractResourcesEndpointMethods(
258+
checker,
259+
clientClass,
260+
makeEndpoint(
261+
'roles',
262+
'https://www.datocms.com/docs/content-management-api/resources/role/update',
263+
),
264+
);
265+
266+
const names = methods.map((m) => m.name).sort();
267+
expect(names).toContain('update');
268+
expect(names).toContain('rawUpdate');
269+
expect(names).toContain('updateCurrentEnvironmentPermissions');
270+
}, 10000);
271+
272+
it('returns empty array for an unknown docUrl', () => {
273+
const { checker, clientClass } = cma;
274+
275+
const methods = extractResourcesEndpointMethods(
276+
checker,
277+
clientClass,
278+
makeEndpoint('items', 'https://example.invalid/does/not/exist'),
279+
);
280+
281+
expect(methods).toEqual([]);
282+
}, 10000);
283+
284+
it('returns empty array for an unknown namespace', () => {
285+
const { checker, clientClass } = cma;
286+
287+
const methods = extractResourcesEndpointMethods(
288+
checker,
289+
clientClass,
290+
makeEndpoint(
291+
'invalid_resource_xyz',
292+
'https://www.datocms.com/docs/content-management-api/resources/item/instances',
293+
),
294+
);
295+
296+
expect(methods).toEqual([]);
297+
}, 10000);
298+
});
299+
202300
describe('extractTypeDependencies', () => {
203301
it('should handle empty type list', () => {
204302
const { program, checker } = cma;

packages/cma-client/src/resources/Role.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ function applyChanges<T extends Record<PropertyKey, unknown>>(
9595
export default class RoleResource extends BaseRole {
9696
/**
9797
* Applies a set of changes to the permissions of the current environment
98+
*
99+
* Read more: https://www.datocms.com/docs/content-management-api/resources/role/update
98100
*/
99101
async updateCurrentEnvironmentPermissions(
100102
roleId: string | ApiTypes.RoleData,

0 commit comments

Comments
 (0)