Skip to content

Commit f9570ed

Browse files
committed
feat: new implemenation with proxy provider
1 parent 1ad9e6a commit f9570ed

13 files changed

+421
-76
lines changed

jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
22
module.exports = {
33
preset: 'ts-jest',
4-
testEnvironment: 'node',
4+
testEnvironment: 'jsdom',
55
};

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@
138138
"eslint-plugin-typescript-sort-keys": "2.3.0",
139139
"eslint-plugin-unused-imports": "2.0.0",
140140
"husky": "8.0.3",
141+
"isomorphic-fetch": "^3.0.0",
141142
"jest": "29.5.0",
143+
"jest-environment-jsdom": "^29.5.0",
142144
"lint-staged": "13.2.2",
143145
"prettier": "2.8.8",
144146
"prop-types": "15.8.1",

src/example.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22
import { trpc } from './inference/trpc';
33

44
import { usePDFParser } from './parser/use-pdf-parser';
5-
import { LabAIClientProvider } from './inference/provider';
5+
import { LabTrpcProvider } from './inference/trpc-provider';
66

77
export type DemoProps = {
88
text?: String;
@@ -41,12 +41,12 @@ export function Demo(props: DemoProps) {
4141
};
4242

4343
return (
44-
<LabAIClientProvider secret="secret">
44+
<LabTrpcProvider secret="secret">
4545
<input type="file" onChange={handleFileChange} />
4646
<button onClick={handleSubmit} type="button">
4747
{`${props.text}`}
4848
</button>
49-
</LabAIClientProvider>
49+
</LabTrpcProvider>
5050
);
5151
}
5252

src/index.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import 'immer';
22

33
/* Inference and API Interface */
4-
// export { inference } from './inference/actions';
5-
export { trpc, proxy } from './inference/trpc';
6-
export { LabAIClientProvider } from './inference/provider';
4+
export { LabTrpcProvider } from './inference/trpc-provider';
75

86
/** PDF and upload parser hooks */
97
export { usePDFParser } from './parser/use-pdf-parser';
10-
// export { pages } from './parser/store';

src/inference/proxy-provider.tsx

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as React from 'react';
2+
import { createContext, useContext } from 'react';
3+
import { createLabProxy } from './proxy';
4+
5+
export const LabBaseContext = createContext(
6+
createLabProxy({ secretOrKey: '' })
7+
);
8+
9+
type LabProviderProps = {
10+
children: React.ReactNode;
11+
customHttpLink?: string;
12+
secretOrKey: string;
13+
};
14+
15+
export const LabProvider = ({
16+
secretOrKey,
17+
customHttpLink,
18+
children,
19+
}: LabProviderProps) => {
20+
const proxy = createLabProxy({ secretOrKey, customHttpLink });
21+
22+
return (
23+
<LabBaseContext.Provider value={proxy}>{children}</LabBaseContext.Provider>
24+
);
25+
};
26+
27+
export const useIn = () => {
28+
const context = useContext(LabBaseContext);
29+
30+
if (!context) {
31+
throw new Error('useScanner must be used within a LabProvider');
32+
}
33+
34+
return context;
35+
};

src/inference/proxy.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ type DecoratedProcedureRecord<
1717
: never;
1818
};
1919

20-
// @ts-ignore
21-
export const proxy: DecoratedProcedureRecord<any, AnyRouter> =
20+
export type CreateLabProxyOptions = {
21+
customHttpLink?: string;
22+
secretOrKey: string;
23+
};
24+
25+
export const createLabProxy = ({
26+
secretOrKey,
27+
customHttpLink,
28+
}: CreateLabProxyOptions): DecoratedProcedureRecord<any, AnyRouter> =>
29+
// @ts-ignore
2230
createTRPCProxyClient({
2331
links: [
2432
loggerLink({
@@ -28,14 +36,12 @@ export const proxy: DecoratedProcedureRecord<any, AnyRouter> =
2836
}),
2937
httpBatchLink({
3038
// url: `http://localhost:3001/api/trpc`,
31-
url: `https://labai.tecmie.africa/api/trpc`,
39+
url: customHttpLink ?? `https://labai.tecmie.africa/api/trpc`,
3240
/**
3341
* Headers will be called on each request.
3442
*/
3543
headers: () => ({
36-
'x-secret':
37-
process.env.NEXT_PUBLIC_SECRET ||
38-
'sk_P9uPoekyZLV5MEUHCsgPZ4DTmp99m8DUoxy4SpVzSkDsi2azKJ6mhN83',
44+
'x-secret': secretOrKey,
3945
}),
4046
}),
4147
],

src/inference/provider.tsx src/inference/trpc-provider.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ type ProviderProps = {
1010
secret: string;
1111
};
1212

13-
export const LabAIClientProvider = ({
13+
export const LabTrpcProvider = ({
1414
children,
1515
defaultQueryClient,
1616
httpRpcURL,
1717
secret,
1818
}: ProviderProps) => {
1919
const getBaseUrl = () => {
2020
if (typeof window !== 'undefined') return ''; // browser should use relative url
21-
// return httpRpcURL || `https://labai.tecmie.africa`; // dev SSR should use localhost
22-
return httpRpcURL || `http://localhost:3001`; // dev SSR should use localhost
21+
return httpRpcURL || `https://labai.tecmie.africa`; // dev SSR should use localhost
22+
// return httpRpcURL || `http://localhost:3001`; // dev SSR should use localhost
2323
};
2424

2525
const secretOrKey = secret ?? process.env.INFERENCE_SECRET;

tests/Example.test.tsx

-10
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`LabProvider matches snapshot 1`] = `
4+
<DocumentFragment>
5+
<div>
6+
Test child
7+
</div>
8+
</DocumentFragment>
9+
`;

tests/proxy-provider.test.tsx

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as React from 'react';
2+
import { render } from '@testing-library/react';
3+
import { LabProvider } from '../src/inference/proxy-provider';
4+
5+
require('isomorphic-fetch'); // Adjust the path as necessary
6+
7+
test('LabProvider matches snapshot', () => {
8+
const { asFragment } = render(
9+
<LabProvider secretOrKey="test-key">
10+
<div>Test child</div>
11+
</LabProvider>
12+
);
13+
14+
expect(asFragment()).toMatchSnapshot();
15+
});

tests/use-pdf-parser.test.tsx

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
import { renderHook } from '@testing-library/react-hooks';
2-
import { usePDFParser } from '../src/parser/use-pdf-parser';
1+
// import 'pdfjs-dist';
2+
// import { renderHook } from '@testing-library/react-hooks';
3+
// import { usePDFParser } from '../src/parser/use-pdf-parser';
34

4-
describe('usePDFParser', () => {
5-
it('should return the expected object', () => {
6-
const canvasRef = { current: document.createElement('canvas') };
7-
const { result } = renderHook(() => usePDFParser({ canvasRef }));
8-
expect(result.current).toEqual({
9-
document: undefined,
10-
documentURL: '',
11-
executeUpload: expect.any(Function),
12-
parsePageText: expect.any(Function),
13-
pdfPage: undefined,
14-
});
15-
});
5+
// describe('usePDFParser', () => {
6+
// it('should return the expected object', () => {
7+
// const canvasRef = { current: document.createElement('canvas') };
8+
// const { result } = renderHook(() => usePDFParser({ canvasRef }));
9+
// expect(result.current).toEqual({
10+
// document: undefined,
11+
// documentURL: '',
12+
// executeUpload: expect.any(Function),
13+
// parsePageText: expect.any(Function),
14+
// pdfPage: undefined,
15+
// });
16+
// });
17+
// });
18+
19+
describe('Mock usePDFParser', () => {
20+
it.todo('renders children');
1621
});

tests/use-pdf.test.tsx

+30-26
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
// import React from 'react';
22
// import { render, wait, waitForDomChange } from '@testing-library/react';
3-
// import Pdf from '../src';
3+
// import Pdf from '../src/parser/use-pdf';
44
// import { DocumentInitParameters } from 'pdfjs-dist/types/src/display/api';
55

6-
// jest.mock('pdfjs-dist', () => ({
7-
// version: '1.0',
8-
// GlobalWorkerOptions: {
9-
// workerSrc: '',
10-
// },
11-
// getDocument: jest.fn((config: DocumentInitParameters) => ({
12-
// promise: config.url?.includes('fail_document')
13-
// ? Promise.reject()
14-
// : Promise.resolve({
15-
// getPage: jest.fn(() =>
16-
// config.url?.includes('fail_page')
17-
// ? Promise.reject()
18-
// : Promise.resolve({
19-
// getViewport: jest.fn(() => ({ width: 0, height: 0 })),
20-
// render: jest.fn(() => ({
21-
// promise: config.url?.includes('fail_render')
22-
// ? Promise.reject()
23-
// : Promise.resolve(),
24-
// })),
25-
// })
26-
// ),
27-
// }),
28-
// })),
29-
// }));
6+
// // jest.mock('pdfjs-dist', () => ({
7+
// // version: '1.0',
8+
// // GlobalWorkerOptions: {
9+
// // workerSrc: '',
10+
// // },
11+
// // getDocument: jest.fn((config: DocumentInitParameters) => ({
12+
// // promise: config.url?.includes('fail_document')
13+
// // ? Promise.reject()
14+
// // : Promise.resolve({
15+
// // getPage: jest.fn(() =>
16+
// // config.url?.includes('fail_page')
17+
// // ? Promise.reject()
18+
// // : Promise.resolve({
19+
// // getViewport: jest.fn(() => ({ width: 0, height: 0 })),
20+
// // render: jest.fn(() => ({
21+
// // promise: config.url?.includes('fail_render')
22+
// // ? Promise.reject()
23+
// // : Promise.resolve(),
24+
// // })),
25+
// // })
26+
// // ),
27+
// // }),
28+
// // })),
29+
// // }));
3030

3131
// describe('Pdf', () => {
3232
// // it('renders children', async () => {
@@ -94,7 +94,7 @@
9494
// onPageRenderSuccess={onPageRenderSuccess}
9595
// onPageRenderFail={onPageRenderFail}
9696
// >
97-
// {({ canvas }) => canvas}
97+
// {({ canvas }: any) => canvas}
9898
// </Pdf>
9999
// );
100100

@@ -151,3 +151,7 @@
151151
// });
152152
// });
153153
// });
154+
155+
describe('Mock test', () => {
156+
it.todo('renders children');
157+
});

0 commit comments

Comments
 (0)