-
Notifications
You must be signed in to change notification settings - Fork 8
fix(types): add extends to role and resource-role types (PER-16500) #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zeevmoney
merged 3 commits into
permitio:main
from
Kyzgor:fix/add-extends-field-to-role-types
Sep 29, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import test from 'ava'; | ||
|
|
||
| import { Permit } from '../../index'; | ||
|
|
||
| type RoleCreate = Parameters<Permit['api']['roles']['create']>[0]; | ||
| type RoleRead = Awaited<ReturnType<Permit['api']['roles']['get']>>; | ||
| type RoleUpdate = Parameters<Permit['api']['roles']['update']>[1]; | ||
| type ResourceRoleCreate = Parameters<Permit['api']['resourceRoles']['create']>[1]; | ||
| type ResourceRoleRead = Awaited<ReturnType<Permit['api']['resourceRoles']['get']>>; | ||
| type ResourceRoleUpdate = Parameters<Permit['api']['resourceRoles']['update']>[2]; | ||
|
|
||
| type IsExact<A, B> = (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 | ||
| ? true | ||
| : false; | ||
| type Assert<T extends true> = T; | ||
| type Inheritance = { extends?: Array<string> }; | ||
|
|
||
| // yarn test:unit runs build:types before AVA. Pick preserves the optional modifier, and exact | ||
| // equality rejects any, null, non-array values, and a required property (even with undefined). | ||
| export type RoleInheritanceContracts = [ | ||
| Assert<IsExact<Pick<RoleCreate, 'extends'>, Inheritance>>, | ||
| Assert<IsExact<Pick<RoleRead, 'extends'>, Inheritance>>, | ||
| Assert<IsExact<Pick<RoleUpdate, 'extends'>, Inheritance>>, | ||
| Assert<IsExact<Pick<ResourceRoleCreate, 'extends'>, Inheritance>>, | ||
| Assert<IsExact<Pick<ResourceRoleRead, 'extends'>, Inheritance>>, | ||
| Assert<IsExact<Pick<ResourceRoleUpdate, 'extends'>, Inheritance>>, | ||
| ]; | ||
|
|
||
| function createClient(inheritance: Inheritance) { | ||
| const permit = new Permit({ | ||
| token: 'test', | ||
| apiUrl: 'https://api.permit.io', | ||
| log: { level: 'silent' }, | ||
| }); | ||
| const requests: Array<{ method: string | undefined; url: string | undefined; body: unknown }> = | ||
| []; | ||
| const role: ResourceRoleRead = { | ||
| key: 'editor', | ||
| name: 'Editor', | ||
| id: 'role-id', | ||
| organization_id: 'org', | ||
| project_id: 'proj', | ||
| environment_id: 'env', | ||
| resource_id: 'resource-id', | ||
| resource: 'doc', | ||
| created_at: '2026-01-01T00:00:00Z', | ||
| updated_at: '2026-01-01T00:00:00Z', | ||
| ...inheritance, | ||
| }; | ||
| permit.config.axiosInstance.defaults.adapter = async (config) => { | ||
| const isScopeRequest = config.url === 'https://api.permit.io/v2/api-key/scope'; | ||
| if (!isScopeRequest) { | ||
| requests.push({ | ||
| method: config.method, | ||
| url: config.url, | ||
| body: config.data === undefined ? undefined : JSON.parse(config.data), | ||
| }); | ||
| } | ||
| return { | ||
| status: 200, | ||
| statusText: 'OK', | ||
| headers: {}, | ||
| config, | ||
| data: isScopeRequest | ||
| ? { organization_id: 'org', project_id: 'proj', environment_id: 'env' } | ||
| : { ...role }, | ||
| }; | ||
| }; | ||
| return { permit, requests }; | ||
| } | ||
|
|
||
| const cases: Array<{ name: string; inheritance: Inheritance }> = [ | ||
| { name: 'multiple inherited roles', inheritance: { extends: ['viewer', 'auditor'] } }, | ||
| { name: 'empty inheritance', inheritance: { extends: [] } }, | ||
| { name: 'omitted inheritance', inheritance: {} }, | ||
| ]; | ||
|
|
||
| for (const { name, inheritance } of cases) { | ||
| test(`roles create/update/get preserve ${name}`, async (t) => { | ||
| const { permit, requests } = createClient(inheritance); | ||
| const created = await permit.api.roles.create({ | ||
| key: 'editor', | ||
| name: 'Editor', | ||
| ...inheritance, | ||
| }); | ||
| const updated = await permit.api.roles.update('editor', { ...inheritance }); | ||
| const fetched = await permit.api.roles.get('editor'); | ||
|
|
||
| t.deepEqual(created.extends, inheritance.extends); | ||
| t.deepEqual(updated.extends, inheritance.extends); | ||
| t.deepEqual(fetched.extends, inheritance.extends); | ||
| t.deepEqual(requests, [ | ||
| { | ||
| method: 'post', | ||
| url: 'https://api.permit.io/v2/schema/proj/env/roles', | ||
| body: { key: 'editor', name: 'Editor', ...inheritance }, | ||
| }, | ||
| { | ||
| method: 'patch', | ||
| url: 'https://api.permit.io/v2/schema/proj/env/roles/editor', | ||
| body: { ...inheritance }, | ||
| }, | ||
| { | ||
| method: 'get', | ||
| url: 'https://api.permit.io/v2/schema/proj/env/roles/editor', | ||
| body: undefined, | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| test(`resource roles create/update/get preserve ${name}`, async (t) => { | ||
| const { permit, requests } = createClient(inheritance); | ||
| const created = await permit.api.resourceRoles.create('doc', { | ||
| key: 'editor', | ||
| name: 'Editor', | ||
| ...inheritance, | ||
| }); | ||
| const updated = await permit.api.resourceRoles.update('doc', 'editor', { ...inheritance }); | ||
| const fetched = await permit.api.resourceRoles.get('doc', 'editor'); | ||
|
|
||
| t.deepEqual(created.extends, inheritance.extends); | ||
| t.deepEqual(updated.extends, inheritance.extends); | ||
| t.deepEqual(fetched.extends, inheritance.extends); | ||
| t.deepEqual(requests, [ | ||
| { | ||
| method: 'post', | ||
| url: 'https://api.permit.io/v2/schema/proj/env/resources/doc/roles', | ||
| body: { key: 'editor', name: 'Editor', ...inheritance }, | ||
| }, | ||
| { | ||
| method: 'patch', | ||
| url: 'https://api.permit.io/v2/schema/proj/env/resources/doc/roles/editor', | ||
| body: { ...inheritance }, | ||
| }, | ||
| { | ||
| method: 'get', | ||
| url: 'https://api.permit.io/v2/schema/proj/env/resources/doc/roles/editor', | ||
| body: undefined, | ||
| }, | ||
| ]); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.