Ver a proveniência

Refactor API

Improve API of summarizer.
master
ascendente
cometimento
66b3f742b1
4 ficheiros alterados com 19 adições e 22 eliminações
  1. +9
    -6
      src/common.ts
  2. +0
    -6
      src/index.ts
  3. +4
    -7
      src/video-types/youtube/index.ts
  4. +6
    -3
      test/index.test.ts

+ 9
- 6
src/common.ts Ver ficheiro

@@ -6,12 +6,18 @@ export type ProcessEvent = {
contentType?: string,
};

type ProcessEventCallback = (event: ProcessEvent) => void;
export type ProcessEventCallback = (event: ProcessEvent) => void;

type ErrorEventCallback = (event: Error) => void;
export type ErrorEventCallback = (event: Error) => void;

export interface SummarizerProcessParams {
url: string;
language?: string;
country?: string;
}

export interface SummarizerEventEmitter extends NodeJS.EventEmitter {
process(): void;
process<T extends SummarizerProcessParams>(params: T): void;
on(eventType: 'process', callback: ProcessEventCallback): this;
on(eventType: 'error', callback: ErrorEventCallback): this;
on(eventType: 'end', callback: () => void): this;
@@ -25,8 +31,5 @@ export interface OpenAiParams {
}

export interface CreateBaseSummarizerParams {
url: string;
language?: string;
country?: string;
openAiParams: OpenAiParams;
}

+ 0
- 6
src/index.ts Ver ficheiro

@@ -15,19 +15,13 @@ export interface CreateSummarizerParams extends CreateYouTubeSummarizerParams {
export const createSummarizer = (params: CreateSummarizerParams): SummarizerEventEmitter => {
const {
type: videoType,
url,
openAiParams,
language,
country,
} = params;

switch (videoType as string) {
case VideoType.YOUTUBE:
return new YouTubeSummarizerEventEmitter({
url,
openAiParams,
language,
country,
});
default:
break;


+ 4
- 7
src/video-types/youtube/index.ts Ver ficheiro

@@ -1,5 +1,5 @@
import { EventEmitter } from 'events';
import { CreateBaseSummarizerParams, SummarizerEventEmitter } from '../../common';
import { CreateBaseSummarizerParams, SummarizerEventEmitter, SummarizerProcessParams } from '../../common';
import {
retrieveVideoId,
getVideoPage,
@@ -15,12 +15,9 @@ export class YouTubeSummarizerEventEmitter extends EventEmitter implements Summa
super();
}

process() {
const {
url,
openAiParams,
...config
} = this.params;
process(params: SummarizerProcessParams) {
const { url, ...config } = params;
const { openAiParams } = this.params;
const identifier = retrieveVideoId(url);

this.emit('process', {


+ 6
- 3
test/index.test.ts Ver ficheiro

@@ -20,8 +20,9 @@ describe('blah', () => {
it('works', () => new Promise<void>((done) => {
const summarizer = createSummarizer({
type: VideoType.YOUTUBE,
url: 'https://www.youtube.com/watch?v=WeNgDxtBiyw',
openaiApiKey: process.env.OPENAI_API_KEY as string,
openAiParams: {
apiKey: process.env.OPENAI_API_KEY as string,
},
});

summarizer.on('process', (data) => {
@@ -51,6 +52,8 @@ describe('blah', () => {
done();
});

summarizer.process();
summarizer.process({
url: 'https://www.youtube.com/watch?v=WeNgDxtBiyw',
});
}), { timeout: 180000 });
});

Carregando…
Cancelar
Guardar