Skip to content

Commit

Permalink
Makes ToEncryptPaths type safe and fix bug founds thx to it
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarenaldi committed Sep 11, 2023
1 parent 796d141 commit cb14c90
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 53 deletions.
2 changes: 1 addition & 1 deletion packages/gated-content/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"license": "MIT",
"dependencies": {
"@lens-protocol/metadata": "0.1.0-alpha.11",
"@lens-protocol/metadata": "0.1.0-alpha.12",
"@lens-protocol/shared-kernel": "workspace:*",
"@lens-protocol/storage": "workspace:*",
"@lit-protocol/constants": "2.1.62",
Expand Down
19 changes: 4 additions & 15 deletions packages/gated-content/src/__tests__/paths.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as raw from '@lens-protocol/metadata';

import * as gql from '../graphql';
import { resolvePathsToDecrypt, resolvePathsToEncrypt } from '../paths';
import { deriveDecryptionPaths, resolvePathsToDecrypt, resolvePathsToEncrypt } from '../paths';

describe(`Given the paths helpers`, () => {
describe(`when calling the "${resolvePathsToEncrypt.name}" function`, () => {
Expand All @@ -27,7 +27,6 @@ describe(`Given the paths helpers`, () => {
'lens.content',
// 'lens.title' // omitted on purpose to allow to tease potential audience
'lens.audio.item',
'lens.audio.altTag',
'lens.audio.cover',
'lens.audio.credits',
'lens.audio.artist',
Expand Down Expand Up @@ -202,24 +201,14 @@ describe(`Given the paths helpers`, () => {
},
{
schema: raw.PublicationSchemaId.TEXT_ONLY_LATEST,
expected: [
'lens.content',
'lens.attachments[n].item',
'lens.attachments[n].altTag',
'lens.attachments[n].cover',
'lens.attachments[n].credits',
'lens.attachments[n].artist',
'lens.attachments[n].genre',
'lens.attachments[n].recordLabel',
'lens.attachments[n].lyrics',
],
expected: ['lens.content'],
},
{
schema: raw.PublicationSchemaId.THREE_D_LATEST,
expected: [
'lens.content',
'lens.threeDAssets[n].uri',
'lens.threeDAssets[n].playerUrl',
'lens.assets[n].uri',
'lens.assets[n].playerUrl',
'lens.attachments[n].item',
'lens.attachments[n].altTag',
'lens.attachments[n].cover',
Expand Down
39 changes: 23 additions & 16 deletions packages/gated-content/src/paths.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import * as raw from '@lens-protocol/metadata';
import { UnknownObject } from '@lens-protocol/shared-kernel';

import * as gql from './graphql';
import { Paths } from './types';

const ToEncryptPaths: Record<raw.PublicationSchemaId, string[]> = {
type MapOf<
Target extends UnknownObject,
Discriminant extends keyof Target = keyof Target,
DiscriminantValue extends string = Target[Discriminant] extends string
? Target[Discriminant]
: never,
> = {
[P in DiscriminantValue]: Paths<Extract<Target, { [KK in Discriminant]: P }>>[];
};

const ToEncryptPaths: MapOf<raw.PublicationMetadata, '$schema'> = {
[raw.PublicationSchemaId.ARTICLE_LATEST]: [
'lens.content',
'lens.attachments[n].item',
Expand All @@ -17,7 +29,6 @@ const ToEncryptPaths: Record<raw.PublicationSchemaId, string[]> = {
[raw.PublicationSchemaId.AUDIO_LATEST]: [
'lens.content',
'lens.audio.item',
'lens.audio.altTag',
'lens.audio.cover',
'lens.audio.credits',
'lens.audio.artist',
Expand Down Expand Up @@ -163,22 +174,12 @@ const ToEncryptPaths: Record<raw.PublicationSchemaId, string[]> = {
'lens.attachments[n].lyrics',
],

[raw.PublicationSchemaId.TEXT_ONLY_LATEST]: [
'lens.content',
'lens.attachments[n].item',
'lens.attachments[n].altTag',
'lens.attachments[n].cover',
'lens.attachments[n].credits',
'lens.attachments[n].artist',
'lens.attachments[n].genre',
'lens.attachments[n].recordLabel',
'lens.attachments[n].lyrics',
],
[raw.PublicationSchemaId.TEXT_ONLY_LATEST]: ['lens.content'],

[raw.PublicationSchemaId.THREE_D_LATEST]: [
'lens.content',
'lens.threeDAssets[n].uri',
'lens.threeDAssets[n].playerUrl',
'lens.assets[n].uri',
'lens.assets[n].playerUrl',
'lens.attachments[n].item',
'lens.attachments[n].altTag',
'lens.attachments[n].cover',
Expand Down Expand Up @@ -218,7 +219,9 @@ const ToEncryptPaths: Record<raw.PublicationSchemaId, string[]> = {
],
};

export function resolvePathsToEncrypt({ $schema }: raw.PublicationMetadata): string[] {
export function resolvePathsToEncrypt<T extends raw.PublicationMetadata>({
$schema,
}: T): Paths<raw.PublicationMetadata>[] {
if ($schema in ToEncryptPaths) {
return ToEncryptPaths[$schema];
}
Expand Down Expand Up @@ -534,3 +537,7 @@ export function resolvePathsToDecrypt({ __typename }: gql.AnyEncryptedPublicatio
// decrypt it.
return [];
}

export function deriveDecryptionPaths(paths: string[]): string[] {
return paths;
}
16 changes: 16 additions & 0 deletions packages/gated-content/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { UnknownObject } from '@lens-protocol/shared-kernel';
import { Primitive } from 'zod';

import { Maybe } from './graphql';

Expand All @@ -15,3 +16,18 @@ export type Entries<T> = Values<KeyValuePairs<T>>;
export type ExtractFields<T extends UnknownObject, F extends string> = Pick<T, Extract<keyof T, F>>;

export type Mutable<T> = { -readonly [P in keyof T]: T[P] };

type UnionOf<A> = A extends Array<unknown> ? A[number] : A[keyof A];

// Adapted from ts-toolbelt: https://github.com/millsp/ts-toolbelt/blob/master/sources/Object/Paths.ts#L21
export type Paths<Target, Root extends string = ''> = UnionOf<{
[K in keyof Target]: K extends string
? `${Root}${Target[K] extends Primitive | undefined
? K
: Target[K] extends Array<infer ItemType> | undefined
? ItemType extends Primitive // acts also to distribute over union ItemType
? `${K}[n]`
: Paths<ItemType, `${K}[n].`>
: Paths<Target[K], `${K}.`>}`
: never;
}>;
29 changes: 8 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cb14c90

Please sign in to comment.