Skip to content

Commit

Permalink
Adding entitlements and header options system. (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
sleeper-luke authored Aug 31, 2023
1 parent 47c85f6 commit b99e428
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 32 deletions.
7 changes: 5 additions & 2 deletions bin/build_mini.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const getCommands = (projectName) => {
const zip = isWindows ?
`powershell Compress-Archive -Path "${distPath}" -DestinationPath "${zipFilePath}"` :
`cd dist && zip -r "${projectName}.zip" * && cd -`;
const openFile = isWindows ? `start .` : `open .`;
const openFile = isWindows ? `start dist` : `open dist`;
const cleanIndex = `${removeDir} "${bundleOutputPath["ios"]}" "${bundleOutputPath["android"]}" "${sourcemapOutputPath["ios"]}" "${sourcemapOutputPath["android"]}"`;
const cleanAll = `${removeDir} dist node_modules`;
const cleanBuild = `${removeDir} "${distPath}" "${assetsDestPath["ios"]}" "${assetsDestPath["android"]}"`;
Expand Down Expand Up @@ -139,7 +139,7 @@ const validateProjectName = (name) => {
const main = async () => {
// Enter project name.
const fallback = getProjectName();
const projectName = await getInput("Enter project name: ", fallback);
const projectName = await getInput("Enter project name (return to skip): ", fallback);
if (!validateProjectName(projectName)) {
printError("Invalid project name. Only alphanumeric characters and underscores are allowed.");
process.exit(1);
Expand Down Expand Up @@ -168,6 +168,9 @@ const main = async () => {
await spawnProcess(commands.bundleAndroid, "webpack-bundle android command exited with non-zero code");
printComplete("Android build complete.");

// Copy the app.json file to the dist folder
fs.copyFileSync('app.json', path.join('dist', projectName, 'app.json'));

// Create Zip Archive
printInfo("Creating zip archive...");
await spawnProcess(commands.cleanIndex, "clean command exited with non-zero code");
Expand Down
3 changes: 3 additions & 0 deletions declarations/sleeper_entitlement.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare type SleeperEntitlement = 'user:email' | 'user:phone' | 'wallet:date_of_birth' | 'wallet:first_name' | 'wallet:last_name' | 'wallet:country_code' | 'wallet:city' | 'location:longitude' | 'location:latitude' | 'location:state' | 'location:country' | 'location:postalCode';

export default SleeperEntitlement;
5 changes: 5 additions & 0 deletions declarations/sleeper_header_options.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare class SleeperHeaderOptions {
useLeagueSelector?: boolean;
}

export default SleeperHeaderOptions;
5 changes: 5 additions & 0 deletions declarations/sleeper_message.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import type SleeperEntitlement from "@sleeperhq/mini-core/declarations/sleeper_entitlement";
import type SleeperHeaderOptions from "@sleeperhq/mini-core/declarations/sleeper_header_options";

type SocketMessage = {
_ip?: string;
_name?: string;
_webpack?: string;
_contextGet?: string;
_entitlements?: SleeperEntitlement[];
_headerOptions?: SleeperHeaderOptions;
};
export default SocketMessage;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sleeperhq/mini-core",
"version": "1.4.6",
"version": "1.5.2",
"description": "Core library frameworks for developing Sleeper Mini Apps.",
"main": "index.ts",
"types": "index.d.ts",
Expand Down
14 changes: 9 additions & 5 deletions src/dev_server/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ const DevServer = props => {

if (messageType.current === 'context') {
// We should have a context object now
const context = new Proxy(json, handler);
props.onContextChanged(context);
const context = new Proxy(json._context, handler);
props.onContextChanged(context, json._entitlements);
} else if (messageType.current === `partialContext`) {
// We are updating a partial Context
props.onContextUpdated(json)
props.onContextUpdated(json._context);
} else if (messageType.current === 'entitlements') {
props.onEntitlementsUpdated(json._entitlements);
}

messageType.current = '';
Expand Down Expand Up @@ -196,10 +198,12 @@ const DevServer = props => {
localAddress: ipAddress,
reuseAddress: true,
}, () => {
// When we establish a connection, send the IP address to the server
// When we establish a connection, send some data to the server
const message: SocketMessage = {
_ip: ipAddress,
_name: config.name,
_name: config.name,
_entitlements: config.entitlements,
_headerOptions: config.headerOptions,
};
const json = JSON.stringify(message);
console.log('[Sleeper] Send IP address: ', ipAddress, config.name);
Expand Down
9 changes: 8 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type SleeperEntitlement from '../../declarations/sleeper_entitlement.d';
import type SleeperHeaderOptions from '../../declarations/sleeper_header_options.d';

export * from '../../declarations/types/index.d';
export { default as Context } from '../../declarations/sleeper_context.d';
export type { default as Context } from '../../declarations/sleeper_context.d';
export type { default as Entitlements } from '../../declarations/sleeper_entitlement.d';
export type { default as HeaderOptions } from '../../declarations/sleeper_header_options.d';
export type { default as SocketMessage } from '../../declarations/sleeper_message.d';

export type Config = {
Expand All @@ -9,4 +14,6 @@ export type Config = {
remoteSocketPort?: number,
dev?: boolean,
logsEnabled?: boolean,
entitlements?: SleeperEntitlement[],
headerOptions?: SleeperHeaderOptions,
};
23 changes: 17 additions & 6 deletions start.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ DevServer.init(config);
const Root = () => {
const [context, setContext] = useState<Types.Context>({} as Types.Context);
const [connected, setConnected] = useState<boolean>(false);
const [entitlements, setEntitlements] = useState<Types.Entitlements>({} as Types.Entitlements);
const [, updateState] = React.useState<any>();
const forceUpdate = React.useCallback(() => updateState({}), []);

const _onContextChanged = useCallback((data: Types.Context) => {
setContext(data);
const _onContextChanged = useCallback((context: Types.Context, entitlements: Types.Entitlements) => {
setContext(context);
setEntitlements(entitlements);
}, []);

const _onContextUpdated = useCallback(
(data: any) => {
(context: any) => {
setContext(existing => {
for (const key in data) {
existing[key] = data[key];
for (const key in context) {
existing[key] = context[key];
}
return existing;
});
Expand All @@ -38,6 +40,14 @@ const Root = () => {
[forceUpdate],
);

const _onEntitlementsUpdated = useCallback(
(entitlements: any) => {
setEntitlements(entitlements);
forceUpdate();
},
[forceUpdate],
);

const _onConnected = useCallback((value: boolean) => {
setConnected(value);
}, []);
Expand All @@ -59,9 +69,10 @@ const Root = () => {
<DevServer
onContextChanged={_onContextChanged}
onContextUpdated={_onContextUpdated}
onEntitlementsUpdated={_onEntitlementsUpdated}
onConnected={_onConnected}
/>
{connected && <Project context={context} />}
{connected && <Project context={context} entitlements={entitlements} />}
{!connected && _renderWaitingForConnection()}
</SafeAreaView>
);
Expand Down
Loading

0 comments on commit b99e428

Please sign in to comment.