Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
added event types (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirgur authored Jun 27, 2024
1 parent 463d88c commit df5970b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Descope.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { baseHeaders } from './constants';
import { RequestConfig } from '@descope/core-js-sdk';
import { computed } from 'vue';
import { getGlobalSdk } from './sdk';
import type { JWTResponse, ErrorResponse } from './types';

DescopeWcClass.sdkConfigOverrides = {
// Overrides the web-component's base headers to indicate usage via the React SDK
Expand Down Expand Up @@ -93,7 +94,12 @@ const props = defineProps({
type: Object
}
});
const emit = defineEmits(['success', 'error', 'ready']);
// const emit = defineEmits(['success', 'error', 'ready']);
const emit = defineEmits<{
(e: 'success', payload: CustomEvent<JWTResponse>): void;
(e: 'error', payload: CustomEvent<ErrorResponse>): void;
(e: 'ready', payload: CustomEvent<Record<string, never>>): void;
}>();
const { projectId, baseUrl, baseStaticUrl, storeLastAuthenticatedUser } =
useOptions();
const sdk = useDescope();
Expand All @@ -102,15 +108,15 @@ const formStr = computed(() => (props.form ? JSON.stringify(props.form) : ''));
const clientStr = computed(() =>
props.client ? JSON.stringify(props.client) : ''
);
const onSuccess = async (e: CustomEvent) => {
const onSuccess = async (e: CustomEvent<JWTResponse>) => {
await sdk.httpClient.hooks?.afterRequest?.(
{} as RequestConfig,
new Response(JSON.stringify(e.detail))
);
emit('success', e);
};

const onError = (e: Event) => emit('error', e);
const onError = (e: CustomEvent<ErrorResponse>) => emit('error', e);

const onReady = (e: Event) => emit('ready', e);
const onReady = (e: CustomEvent<Record<string, never>>) => emit('ready', e);
</script>
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export type Options = {

export type Sdk = ReturnType<typeof createSdk>;

type FlowResponse = Awaited<ReturnType<Sdk['flow']['next']>>;

export type ErrorResponse = FlowResponse['error'];

export type JWTResponse = FlowResponse['data']['authInfo'];

export type UserData = Exclude<
Awaited<ReturnType<Sdk['me']>>['data'],
undefined
Expand Down

0 comments on commit df5970b

Please sign in to comment.