Skip to content
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misskey-js: admin/roles/createのpoliciesのスキーマを具現化する #14161

Closed
1 task
KisaragiEffective opened this issue Jul 9, 2024 · 7 comments · Fixed by #14167
Closed
1 task
Labels
✨Feature This adds/improves/enhances a feature packages/misskey-js

Comments

@KisaragiEffective
Copy link
Collaborator

KisaragiEffective commented Jul 9, 2024

Summary

admin/roles/createpoliciesRecord<string, never>ではない

Purpose

backend/test/e2e/note.tsで実際に使用されていて、基礎ロールに対するオーバーライドとして機能しているため:

policies: {
alwaysMarkNsfw: {
useDefault: false,
priority: 0,
value: true,
},
} as any,

Do you want to implement this feature yourself?

  • Yes, I will implement this by myself and send a pull request
@KisaragiEffective
Copy link
Collaborator Author

KisaragiEffective commented Jul 9, 2024

なぜRecord<string, never>になってしまうのか意味不明:

policies: {
type: 'object',
},

openapi-typescriptのupstream issue (works as intentional): openapi-ts/openapi-typescript#1686

@KisaragiEffective KisaragiEffective changed the title misskey-js: admin/roles/createのpoliciesはRecord<string, never>ではない misskey-js: admin/roles/createのpoliciesのスキーマを具現化する Jul 9, 2024
@KisaragiEffective
Copy link
Collaborator Author

@kakkokari-gtyih RolePoliciesをうまいことopenapi-typescriptに露出させたいのですが、参照の組み込み方がわからないので教えていただけると幸いです 🙏🏻

@kakkokari-gtyih
Copy link
Contributor

@kakkokari-gtyih RolePoliciesをうまいことopenapi-typescriptに露出させたいのですが、参照の組み込み方がわからないので教えていただけると幸いです 🙏🏻

多分この辺を読んでいただくのが良いかと(Blobはこの通りにやった)
https://openapi-ts.dev/6.x/node#transform-posttransform

@KisaragiEffective
Copy link
Collaborator Author

related: #14147

@KisaragiEffective
Copy link
Collaborator Author

diff --git a/packages/backend/src/server/api/endpoints/admin/roles/create.ts b/packages/backend/src/server/api/endpoints/admin/roles/create.ts
index e0c02f7a5d..5853a69b02 100644
--- a/packages/backend/src/server/api/endpoints/admin/roles/create.ts
+++ b/packages/backend/src/server/api/endpoints/admin/roles/create.ts
@@ -6,7 +6,8 @@
 import { Inject, Injectable } from '@nestjs/common';
 import { Endpoint } from '@/server/api/endpoint-base.js';
 import { RoleEntityService } from '@/core/entities/RoleEntityService.js';
-import { RoleService } from '@/core/RoleService.js';
+import { type RolePolicies, RoleService } from '@/core/RoleService.js';
+import { MiRole } from "@/models/Role.js";
 
 export const meta = {
 	tags: ['admin', 'role'],
@@ -59,8 +60,31 @@ export const paramDef = {
 	],
 } as const;
 
+type RequiredKeys<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? never : K }[keyof T];
+
+type Compile<T> = T extends boolean
+	? { readonly type: 'boolean' }
+	: T extends Record<string, unknown>
+		? Readonly<{
+			type: 'object',
+			properties: Readonly<{[k in keyof T]: Compile<T[k]>}>
+			required: readonly RequiredKeys<T>[]
+		}>
+		: T extends string
+			? { readonly type: 'string' }
+			: T extends number
+				? { readonly type: 'number' }
+				: never;
+
+type Values<T extends Record<PropertyKey, unknown>> = T[keyof T];
+
+// Omitしないとanyに化ける
+type PartialRolePolicy = Partial<{[k in keyof RolePolicies]: Omit<Values<MiRole["policies"]>, "value"> & { value: RolePolicies[k] }}>;
+
+type PartialRolePolicyOverrideSchema = Compile<{ policies: PartialRolePolicy }>;
+
 @Injectable()
-export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
+export default class extends Endpoint<typeof meta, typeof paramDef & PartialRolePolicyOverrideSchema> { // eslint-disable-line import/no-default-export
 	constructor(
 		private roleEntityService: RoleEntityService,
 		private roleService: RoleService,

型システム的にはこれでいいが、ajvに引き渡すために値が必要なのでもうすこし工夫する必要がある

@kakkokari-gtyih
Copy link
Contributor

misskey-jsの表面上で型を変えるだけでいいならpackages/misskey-js/src/api.types.tsでオーバーライドできる

@KisaragiEffective
Copy link
Collaborator Author

#14167

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨Feature This adds/improves/enhances a feature packages/misskey-js
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants