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

chore: typecheck on build #267

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion flagsmith-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ const Flagsmith = class {
if(!response || previousIdentity !== `${this.getContext().identity}`) {
return // getJSON returned null due to request/response mismatch
}
let { flags: features, traits, identifier }: IFlagsmithResponse = response
let { flags: features, traits }: IFlagsmithResponse = response
const {identifier} = response
this.isLoading = false;
// Handle server response
const flags: IFlags = {};
Expand Down
22 changes: 6 additions & 16 deletions move-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,19 @@ const replaceInFileSync = (file, from, to) => {
fs.writeFileSync(file, newData, { encoding: "utf8" });
}


fs.rmSync(path.join(__dirname,"lib/flagsmith/index-es.d.ts"))
fs.rmSync(path.join(__dirname,"lib/flagsmith-es/index-es.d.ts"))

fs.rmSync(path.join(__dirname,"lib/flagsmith/isomorphic-es.d.ts"))
fs.rmSync(path.join(__dirname,"lib/flagsmith-es/isomorphic-es.d.ts"))

fs.rmSync(path.join(__dirname,"lib/react-native-flagsmith/index-es.d.ts"))
fs.rmSync(path.join(__dirname,"lib/react-native-flagsmith/isomorphic-es.d.ts"))


// Copy source files to lib/flagsmith/src
fs.copyFileSync(path.join(__dirname,"index.ts"),path.join(__dirname,"lib/flagsmith/src/index.ts"))
fs.copyFileSync(path.join(__dirname,"flagsmith-core.ts"),path.join(__dirname,"lib/flagsmith/src/flagsmith-core.ts"))
fs.copyFileSync(path.join(__dirname,"next-middleware.ts"),path.join(__dirname,"lib/flagsmith/src/next-middleware.ts"))
fs.copyFileSync(path.join(__dirname,"isomorphic.ts"),path.join(__dirname,"lib/flagsmith/src/isomorphic.ts"))
fs.copyFileSync(path.join(__dirname,"react.tsx"),path.join(__dirname,"lib/flagsmith/src/react.tsx"))
fs.copyFileSync(path.join(__dirname,"react.tsx"),path.join(__dirname,"lib/react-native-flagsmith/src/react.tsx"))
fs.copyFileSync(path.join(__dirname,"react.d.ts"),path.join(__dirname,"lib/react-native-flagsmith/src/react.d.ts"))
//Remove not needed flagsmith-core.d.ts
fs.rmSync(path.join(__dirname,"lib/flagsmith/flagsmith-core.d.ts"))
fs.rmSync(path.join(__dirname,"lib/react-native-flagsmith/flagsmith-core.d.ts"))
fs.rmSync(path.join(__dirname,"lib/flagsmith-es/flagsmith-core.d.ts"))
fs.copyFileSync(path.join(__dirname,"react.d.ts"),path.join(__dirname,"lib/react-native-flagsmith/react.d.ts"))
fs.copyFileSync(path.join(__dirname,"react.d.ts"),path.join(__dirname,"lib/flagsmith/react.d.ts"))
fs.copyFileSync(path.join(__dirname,"react.d.ts"),path.join(__dirname,"lib/flagsmith-es/react.d.ts"))
fs.copyFileSync(path.join(__dirname,"index.d.ts"),path.join(__dirname,"lib/react-native-flagsmith/index.d.ts"))
fs.copyFileSync(path.join(__dirname,"index.d.ts"),path.join(__dirname,"lib/flagsmith/index.d.ts"))
fs.copyFileSync(path.join(__dirname,"index.d.ts"),path.join(__dirname,"lib/flagsmith-es/index.d.ts"))

// Copy source files to lib/react-native-flagsmith/src
fs.copyFileSync(path.join(__dirname,"index.react-native.ts"),path.join(__dirname,"lib/react-native-flagsmith/src/index.react-native.ts"))
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"description": "Feature flagging to support continuous development",
"main": "./flagsmith/index.js",
"scripts": {
"build": "npm run generatetypes && npm run bundle",
"typecheck": "tsc",
"build": "npm run generatetypes && npm run bundle && npm run typecheck",
"bundle": "rollup -c && node ./move-react.js",
"deploy": "npm run build && npm test && cd ./lib/flagsmith/ && npm publish && cd ../../lib/react-native-flagsmith && npm publish && cd ../../lib/flagsmith-es && npm publish",
"deploy:beta": "npm run build && npm test && cd ./lib/flagsmith/ && npm publish --tag beta && cd ../../lib/react-native-flagsmith && npm publish --tag beta && cd ../../lib/flagsmith-es && npm publish --tag beta",
Expand Down Expand Up @@ -55,7 +56,7 @@
"quicktype": "^23.0.170",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rollup": "^2.79.2",
"rollup": "^2.77.0",
"rollup-plugin-dts": "^4.2.0",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-terser": "^7.0.2",
Expand Down
1 change: 1 addition & 0 deletions react.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export declare function useFlags<F extends string, T extends string>(_flags: rea
[K in T]: IFlagsmithTrait;
};
export declare const useFlagsmith: () => IFlagsmith;
export declare const useFlagsmithLoading: () => LoadingState | undefined;
3 changes: 3 additions & 0 deletions react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const FlagsmithProvider: FC<FlagsmithContextType> = ({
const firstRenderRef = useRef(true)
if (flagsmith && !flagsmith?._trigger) {
flagsmith._trigger = () => {
// @ts-expect-error using internal function, consumers would never call this
flagsmith.log("React - trigger event received")
events.emit('event');
}
Expand Down Expand Up @@ -138,6 +139,7 @@ export function useFlags<F extends string=string, T extends string=string>(_flag
const eventListener = useCallback(() => {
const newRenderKey = getRenderKey(flagsmith as IFlagsmith, flags, traits)
if (newRenderKey !== renderRef) {
// @ts-expect-error using internal function, consumers would never call this
flagsmith?.log("React - useFlags flags and traits have changed")
setRenderRef(newRenderKey)
}
Expand All @@ -148,6 +150,7 @@ export function useFlags<F extends string=string, T extends string=string>(_flag

if (firstRender.current) {
firstRender.current = false;
// @ts-expect-error using internal function, consumers would never call this
flagsmith?.log("React - Initialising event listeners")
}

Expand Down
1 change: 1 addition & 0 deletions test/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ describe('Cache', () => {
AsyncStorage: storage,
});
expect(flagsmith.getAllTraits()).toEqual({
//@ts-ignore
...identityState.traits,
ts,
});
Expand Down
6 changes: 3 additions & 3 deletions test/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('Flagsmith.init', () => {
const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({ onChange });
await flagsmith.init(initConfig);

expect(flagsmith.environmentID).toBe(initConfig.environmentID);
expect(flagsmith.getContext().environment?.apiKey).toBe(initConfig.evaluationContext?.environment?.apiKey);
expect(flagsmith.api).toBe('https://edge.api.flagsmith.com/api/v1/'); // Assuming defaultAPI is globally defined
expect(AsyncStorage.getItem).toHaveBeenCalledTimes(1);
expect(mockFetch).toHaveBeenCalledTimes(1);
Expand All @@ -32,7 +32,7 @@ describe('Flagsmith.init', () => {
});
await flagsmith.init(initConfig);

expect(flagsmith.environmentID).toBe(initConfig.environmentID);
expect(flagsmith.getContext().environment?.apiKey).toBe(initConfig.evaluationContext?.environment?.apiKey);
expect(flagsmith.api).toBe('https://edge.api.flagsmith.com/api/v1/'); // Assuming defaultAPI is globally defined
expect(AsyncStorage.getItem).toHaveBeenCalledTimes(1);
expect(mockFetch).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -63,7 +63,7 @@ describe('Flagsmith.init', () => {

await flagsmith.init(initConfig);

expect(flagsmith.environmentID).toBe(initConfig.environmentID);
expect(flagsmith.getContext().environment?.apiKey).toBe(initConfig.evaluationContext?.environment?.apiKey);
expect(flagsmith.api).toBe('https://edge.api.flagsmith.com/api/v1/'); // Assuming defaultAPI is globally defined
expect(AsyncStorage.getItem).toHaveBeenCalledTimes(1);
expect(mockFetch).toHaveBeenCalledTimes(1);
Expand Down
7 changes: 3 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
"skipLibCheck": true,
"jsx": "react",
"module": "ESNext",
"declaration": true,
"declaration": false,
"strict": true,
"declarationDir": ".",
"noEmit": true,
"sourceMap": true,
"moduleResolution": "node",
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true
},
"exclude": ["examples","flagsmith"]
"exclude": ["lib"]
}
12 changes: 8 additions & 4 deletions utils/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,20 @@ class Emitter {
return offFunction.bind(this) as () => this;
}

once(name: EventName, callback: () => void, ctx?: any): () => this {
once(
name: string, // EventName inlined as string
callback: (...args: any[]) => void,
ctx?: any
): () => this {
const self = this;
const id = this.generateCallbackId();

function listener(this: any) {
function listener(this: unknown, ...args: any[]) {
self.off(name, id);
callback.apply(ctx, arguments);
callback.apply(ctx, args);
kyle-ssg marked this conversation as resolved.
Show resolved Hide resolved
}

listener._ = callback;
(listener as any)._ = callback;

return this.on(name, listener, ctx) as () => this;
}
Expand Down
Loading