Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 27 additions & 1 deletion src/booru/booru.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ConfigService } from '@nestjs/config'
import { BooruTypesStringEnum } from '@alejandroakbal/universal-booru-wrapper'
import { BooruService } from './booru.service'
import { booruQueriesDTO } from './dto/booru-queries.dto'
import { BooruEndpointParamsDTO } from './dto/request-booru.dto'
import { BooruEndpointParamsDTO, SupportedBooruType } from './dto/request-booru.dto'
import { BooruAuthManagerService } from './services/booru-auth-manager.service'

describe('BooruService', () => {
Expand Down Expand Up @@ -123,4 +123,30 @@ describe('BooruService', () => {


})

describe('Booru type resolution', () => {
it('should resolve kemono when supported and fail clearly otherwise', () => {
const maybeKemonoClass = (require('@alejandroakbal/universal-booru-wrapper') as any).Kemono

const params: BooruEndpointParamsDTO = {
booruType: 'kemono' as SupportedBooruType
}

const queries = {
baseEndpoint: 'kemono.cr'
} as booruQueriesDTO

if (maybeKemonoClass) {
const api = service.buildApiClass(params, queries)

expect(api).toBeInstanceOf(maybeKemonoClass)

return
}

expect(() => service.buildApiClass(params, queries)).toThrow(
'Kemono booru type requires @alejandroakbal/universal-booru-wrapper version that exports Kemono'
)
})
})
})
17 changes: 15 additions & 2 deletions src/booru/booru.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import {
Rule34PahealNet,
Rule34Xxx
} from '@alejandroakbal/universal-booru-wrapper'
import * as UBW from '@alejandroakbal/universal-booru-wrapper'
import { booruQueriesDTO } from './dto/booru-queries.dto'
import { BooruEndpointParamsDTO } from './dto/request-booru.dto'
import { BooruEndpointParamsDTO, SupportedBooruType } from './dto/request-booru.dto'
import { BooruAuthManagerService } from './services/booru-auth-manager.service'

@Injectable()
Expand Down Expand Up @@ -112,7 +113,7 @@ export class BooruService {
return {}
}

private getApiClassByType(booruType: BooruTypesStringEnum) {
private getApiClassByType(booruType: SupportedBooruType) {
switch (booruType) {
case BooruTypesStringEnum.DANBOORU:
Comment on lines +116 to 118
return Danbooru
Expand Down Expand Up @@ -140,6 +141,18 @@ export class BooruService {

case BooruTypesStringEnum.REALBOORU_COM:
return RealBooruCom

case 'kemono': {
const maybeKemonoClass = (UBW as any).Kemono

if (!maybeKemonoClass) {
throw new Error(
'Kemono booru type requires @alejandroakbal/universal-booru-wrapper version that exports Kemono'
)
}

return maybeKemonoClass
}
}
}
}
8 changes: 6 additions & 2 deletions src/booru/dto/request-booru.dto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { BooruTypesStringEnum } from '@alejandroakbal/universal-booru-wrapper'
import { IsDefined, IsIn, IsNotEmpty, IsString } from 'class-validator'

const BooruTypesToArray = Object.values(BooruTypesStringEnum)
const AdditionalBooruTypes = ['kemono'] as const

export type SupportedBooruType = BooruTypesStringEnum | (typeof AdditionalBooruTypes)[number]

const BooruTypesToArray = [...Object.values(BooruTypesStringEnum), ...AdditionalBooruTypes]

export class BooruEndpointParamsDTO {
@IsDefined()
@IsNotEmpty()
@IsString()
@IsIn(BooruTypesToArray)
readonly booruType: BooruTypesStringEnum
readonly booruType: SupportedBooruType
}
Loading