Skip to content

Commit

Permalink
Add isSandbox, handleExpiredPushTokens and handlePushTokenErrors (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertherber authored Jun 16, 2024
1 parent ecd192e commit 8c78110
Show file tree
Hide file tree
Showing 11 changed files with 519 additions and 461 deletions.
6 changes: 6 additions & 0 deletions .changeset/chilled-ties-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@zemble/core": patch
"@zemble/push-apple": patch
---

Add isSandbox, handleExpiredPushTokens and handlePushTokenErrors
17 changes: 10 additions & 7 deletions packages/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@ export interface PushMessage {
readonly launchImageName?: string;
}

export type PushTokenWithContents<TPush> = {
export type PushTokenWithContents<TPush extends AnyPushTokenWithMetadata> = {
readonly pushToken: TPush
readonly contents: PushMessage | LiveActivityPushProps | Record<string, JSON>
}

export type PushTokenWithContentsAndTicket<TPush> = PushTokenWithContents<TPush> & { readonly ticketId: string }
export type PushTokenWithContentsAndTicket<TPush extends AnyPushTokenWithMetadata> = PushTokenWithContents<TPush> & { readonly ticketId: string }

export type PushTokenWithContentsAndTicketAndLiveActivityId<TPush> = PushTokenWithContents<TPush> & { readonly ticketId: string, readonly liveActivityId: string }
export type PushTokenWithContentsAndTicketAndLiveActivityId<TPush extends AnyPushTokenWithMetadata> = PushTokenWithContents<TPush> & { readonly ticketId: string, readonly liveActivityId: string }

export type PushTokenWithContentsAndFailedReason<TPush> = PushTokenWithContents<TPush> & { readonly failedReason: string }
export type PushTokenWithContentsAndFailedReason<TPush extends AnyPushTokenWithMetadata> = PushTokenWithContents<TPush> & { readonly failedReason: string }

export interface SendPushResponse<TPush, TSuccess = PushTokenWithContentsAndTicket<TPush>> {
export interface SendPushResponse<TPush extends AnyPushTokenWithMetadata, TSuccess = PushTokenWithContentsAndTicket<TPush>> {
readonly failedSendsToRemoveTokensFor: readonly TPush[]
readonly failedSendsOthers: readonly PushTokenWithContentsAndFailedReason<TPush>[]
readonly successfulSends: readonly TSuccess[]
Expand All @@ -127,11 +127,11 @@ export type SendSilentPushProvider = (
pushTokens: readonly PushTokenWithMetadata[],
data: Record<string, JSON>
) => PromiseOrValue<SendPushResponse<PushTokenWithMetadata>>
export type SendStartLiveActivityPushProvider<TPush = PushTokenForStartingLiveActivityWithMetadata> = (
export type SendStartLiveActivityPushProvider<TPush extends PushTokenForStartingLiveActivityWithMetadata = PushTokenForStartingLiveActivityWithMetadata> = (
pushTokens: readonly TPush[],
liveActivity: Omit<LiveActivityPushProps, 'event'> & PushMessage
) => PromiseOrValue<SendPushResponse<TPush>>
export type SendUpdateLiveActivityPushProvider<TPush = PushTokenForUpdatingLiveActivityWithMetadata> = (
export type SendUpdateLiveActivityPushProvider<TPush extends PushTokenForUpdatingLiveActivityWithMetadata = PushTokenForUpdatingLiveActivityWithMetadata> = (
pushTokens: readonly TPush[],
liveActivity: Omit<LiveActivityPushProps, 'attributesType' | 'attributes'>
) => PromiseOrValue<SendPushResponse<TPush>>
Expand Down Expand Up @@ -267,10 +267,13 @@ export interface BaseToken extends JWTPayload
readonly sub: string
}

// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
export type TokenContents = Zemble.TokenRegistry[keyof Zemble.TokenRegistry] & BaseToken | BaseToken
export type PushTokenWithMetadata = Zemble.PushTokenRegistry[keyof Zemble.PushTokenRegistry]
export type PushTokenForStartingLiveActivityWithMetadata = Zemble.PushTokenStartLiveActivityRegistry[keyof Zemble.PushTokenStartLiveActivityRegistry]
export type PushTokenForUpdatingLiveActivityWithMetadata = Zemble.PushTokenUpdateLiveActivityRegistry[keyof Zemble.PushTokenUpdateLiveActivityRegistry]
// eslint-disable-next-line @typescript-eslint/no-duplicate-type-constituents, @typescript-eslint/no-redundant-type-constituents
export type AnyPushTokenWithMetadata = PushTokenWithMetadata | PushTokenForStartingLiveActivityWithMetadata | PushTokenForUpdatingLiveActivityWithMetadata

export type Dependency = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
348 changes: 348 additions & 0 deletions packages/push-apple/clients/apns.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import pushExpoPlugin from '../../plugin'

import type { ApplePushTokenWithMetadata } from '../../types'
import type { MutationResolvers } from '../schema.generated'

const registerApplePushToken: MutationResolvers['registerApplePushToken'] = async (_, { platform, token, appBundleId }, { decodedToken }) => {
const pushTokenWithMetadata: Zemble.ApplePushTokenWithMetadata = {
const registerApplePushToken: MutationResolvers['registerApplePushToken'] = async (_, {
platform, token, appBundleId, isSandbox,
}, { decodedToken }) => {
const pushTokenWithMetadata: ApplePushTokenWithMetadata = {
type: 'APPLE',
platform,
pushToken: token,
appBundleId,
isSandbox: isSandbox ?? false,
}

await pushExpoPlugin.config.persistPushToken(decodedToken!, pushTokenWithMetadata)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import pushExpoPlugin from '../../plugin'
import { ApplePushPlatform, type MutationResolvers } from '../schema.generated'

import type { AppleStartLiveActivityPushTokenWithMetadata } from '../../types'

const registerAppleStartLiveActivityPushToken: MutationResolvers['registerAppleStartLiveActivityPushToken'] = async (_, {
token, appBundleId,
token, appBundleId, isSandbox,
}, { decodedToken }) => {
const pushTokenWithMetadata: Zemble.AppleStartLiveActivityPushTokenWithMetadata = {
const pushTokenWithMetadata: AppleStartLiveActivityPushTokenWithMetadata = {
type: 'APPLE_START_LIVE_ACTIVITY',
platform: ApplePushPlatform.Ios,
pushToken: token,
appBundleId,
isSandbox: isSandbox ?? false,
}

await pushExpoPlugin.config.persistPushToken(decodedToken!, pushTokenWithMetadata)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import pushExpoPlugin from '../../plugin'
import { ApplePushPlatform, type MutationResolvers } from '../schema.generated'

import type { AppleUpdateLiveActivityPushTokenWithMetadata } from '../../types'

const registerAppleUpdateLiveActivityPushToken: MutationResolvers['registerAppleUpdateLiveActivityPushToken'] = async (_, {
token, appBundleId,
token, appBundleId, isSandbox,
}, { decodedToken }) => {
const pushTokenWithMetadata: Zemble.AppleUpdateLiveActivityPushTokenWithMetadata = {
const pushTokenWithMetadata: AppleUpdateLiveActivityPushTokenWithMetadata = {
type: 'APPLE_UPDATE_LIVE_ACTIVITY',
platform: ApplePushPlatform.Ios,
pushToken: token,
appBundleId,
isSandbox: isSandbox ?? false,
}

await pushExpoPlugin.config.persistPushToken(decodedToken!, pushTokenWithMetadata)
Expand Down
4 changes: 3 additions & 1 deletion packages/push-apple/graphql/schema.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,22 @@ export type Mutation = {

export type MutationRegisterApplePushTokenArgs = {
appBundleId: Scalars['String']['input'];
isSandbox?: InputMaybe<Scalars['Boolean']['input']>;
platform: ApplePushPlatform;
token: Scalars['String']['input'];
};


export type MutationRegisterAppleStartLiveActivityPushTokenArgs = {
appBundleId: Scalars['String']['input'];
isSandbox?: InputMaybe<Scalars['Boolean']['input']>;
token: Scalars['String']['input'];
};


export type MutationRegisterAppleUpdateLiveActivityPushTokenArgs = {
appBundleId: Scalars['String']['input'];
liveActivityAttributes: Scalars['JSONObject']['input'];
isSandbox?: InputMaybe<Scalars['Boolean']['input']>;
liveActivityType: Scalars['String']['input'];
token: Scalars['String']['input'];
};
Expand Down
14 changes: 12 additions & 2 deletions packages/push-apple/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ enum ApplePushPlatform {
}

type Mutation {
registerApplePushToken(token: String!, platform: ApplePushPlatform!, appBundleId: String!): Boolean!
registerAppleStartLiveActivityPushToken(token: String!, appBundleId: String!): Boolean!
registerApplePushToken(
token: String!,
platform: ApplePushPlatform!,
appBundleId: String!,
isSandbox: Boolean
): Boolean!
registerAppleStartLiveActivityPushToken(
token: String!,
appBundleId: String!,
isSandbox: Boolean
): Boolean!
registerAppleUpdateLiveActivityPushToken(
token: String!,
appBundleId: String!,
liveActivityType: String!
isSandbox: Boolean
): Boolean!
}
Loading

0 comments on commit 8c78110

Please sign in to comment.