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

Introduce StrictKeyOmit #2968

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/lex-cli/src/codegen/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ function genRecordCls(file: SourceFile, nsid: string, lexRecord: LexRecord) {
})
method.addParameter({
name: 'params',
type: `Omit<${toTitleCase(ATP_METHODS.list)}.QueryParams, "collection">`,
type: `StrictKeyOmit<${toTitleCase(ATP_METHODS.list)}.QueryParams, "collection">`,
})
method.setBodyText(
[
Expand All @@ -330,7 +330,7 @@ function genRecordCls(file: SourceFile, nsid: string, lexRecord: LexRecord) {
})
method.addParameter({
name: 'params',
type: `Omit<${toTitleCase(ATP_METHODS.get)}.QueryParams, "collection">`,
type: `StrictKeyOmit<${toTitleCase(ATP_METHODS.get)}.QueryParams, "collection">`,
})
method.setBodyText(
[
Expand All @@ -348,7 +348,7 @@ function genRecordCls(file: SourceFile, nsid: string, lexRecord: LexRecord) {
})
method.addParameter({
name: 'params',
type: `Omit<${toTitleCase(
type: `StrictKeyOmit<${toTitleCase(
ATP_METHODS.create,
)}.InputSchema, "collection" | "record">`,
})
Expand Down Expand Up @@ -380,7 +380,7 @@ function genRecordCls(file: SourceFile, nsid: string, lexRecord: LexRecord) {
// })
// method.addParameter({
// name: 'params',
// type: `Omit<${toTitleCase(ATP_METHODS.put)}.InputSchema, "collection" | "record">`,
// type: `StrictKeyOmit<${toTitleCase(ATP_METHODS.put)}.InputSchema, "collection" | "record">`,
// })
// method.addParameter({
// name: 'record',
Expand All @@ -407,7 +407,7 @@ function genRecordCls(file: SourceFile, nsid: string, lexRecord: LexRecord) {
})
method.addParameter({
name: 'params',
type: `Omit<${toTitleCase(
type: `StrictKeyOmit<${toTitleCase(
ATP_METHODS.delete,
)}.InputSchema, "collection">`,
})
Expand Down Expand Up @@ -444,6 +444,7 @@ const lexiconTs = (project, lexicons: Lexicons, lexiconDoc: LexiconDoc) =>
xrpcImport.addNamedImports([
{ name: 'HeadersMap' },
{ name: 'XRPCError' },
{ name: 'StrictKeyOmit' },
])
}
//= import {ValidationResult, BlobRef} from '@atproto/lexicon'
Expand Down
14 changes: 14 additions & 0 deletions packages/xrpc/src/types.ts
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be a better place for this utility type. I don't know where you keep things like this so I just shoved it here for now

Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,17 @@ export class XRPCInvalidResponseError extends XRPCError {
)
}
}

type IsLiteralKey<K extends PropertyKey> = {
string: string extends K ? 0 : 1
number: number extends K ? 0 : 1
symbol: symbol extends K ? 0 : 1
}[K extends string ? 'string' : K extends number ? 'number' : 'symbol']
Comment on lines +192 to +196
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't 100% complete. It might not handle index signatures with template literal types or string mappings (like Uppercase<string>) but I seriously doubt you care about any of those here.

In a sense, support for number and symbol is already beyond what you need here but it was easy enough to add it so I did it for the completeness' sake


type LiteralKey<T> = keyof {
[K in keyof T as IsLiteralKey<K> extends 1 ? K : never]: K
}

export type StrictKeyOmit<T, K extends LiteralKey<T>> = {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

K extends LiteralKey<T> is only needed for completions - that's something that you don't quite need because this is only used by a generated code. But the same filtering would have to be done elsewhere without this constraint so it feels nice to have it here

[K2 in keyof T as K2 extends K ? never : K2]: T[K2]
}