Skip to content

Commit

Permalink
Update style guide
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBarba committed Sep 19, 2024
1 parent 1f6da5b commit 5018acd
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 109 deletions.
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"javascript": {
"formatter": {
"semicolons": "asNeeded",
"quoteStyle": "single",
"quoteStyle": "double",
"trailingCommas": "all"
}
}
Expand Down
32 changes: 16 additions & 16 deletions src/apns.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { EventEmitter } from 'node:events'
import { type PrivateKey, createSigner } from 'fast-jwt'
import { type RequestInit, type Response, fetch } from 'fetch-http2'
import { Errors } from './errors'
import { type Notification, Priority } from './notifications/notification'
import { EventEmitter } from "node:events"
import { type PrivateKey, createSigner } from "fast-jwt"
import { type RequestInit, type Response, fetch } from "fetch-http2"
import { Errors } from "./errors"
import { type Notification, Priority } from "./notifications/notification"

// APNS version
const API_VERSION = 3

// Signing algorithm for JSON web token
const SIGNING_ALGORITHM = 'ES256'
const SIGNING_ALGORITHM = "ES256"

// Reset our signing token every 55 minutes as reccomended by Apple
const RESET_TOKEN_INTERVAL_MS = 55 * 60 * 1000

export enum Host {
production = 'api.push.apple.com',
development = 'api.sandbox.push.apple.com',
production = "api.push.apple.com",
development = "api.sandbox.push.apple.com",
}

export interface SigningToken {
Expand Down Expand Up @@ -79,31 +79,31 @@ export class ApnsClient extends EventEmitter {
const token = encodeURIComponent(notification.deviceToken)
const url = `https://${this.host}/${API_VERSION}/device/${token}`
const options: RequestInit = {
method: 'POST',
method: "POST",
headers: {
authorization: `bearer ${this._getSigningToken()}`,
'apns-push-type': notification.pushType,
'apns-topic': notification.options.topic ?? this.defaultTopic,
"apns-push-type": notification.pushType,
"apns-topic": notification.options.topic ?? this.defaultTopic,
},
body: JSON.stringify(notification.buildApnsOptions()),
timeout: this.requestTimeout,
keepAlive: this.keepAlive ?? this.pingInterval ?? 5000,
}

if (notification.priority !== Priority.immediate) {
options.headers!['apns-priority'] = notification.priority.toString()
options.headers!["apns-priority"] = notification.priority.toString()
}

const expiration = notification.options.expiration
if (typeof expiration !== 'undefined') {
options.headers!['apns-expiration'] =
typeof expiration === 'number'
if (typeof expiration !== "undefined") {
options.headers!["apns-expiration"] =
typeof expiration === "number"
? expiration.toFixed(0)
: (expiration.getTime() / 1000).toFixed(0)
}

if (notification.options.collapseId) {
options.headers!['apns-collapse-id'] = notification.options.collapseId
options.headers!["apns-collapse-id"] = notification.options.collapseId
}

const res = await fetch(url, options)
Expand Down
64 changes: 32 additions & 32 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
export enum Errors {
badCertificate = 'BadCertificate',
badCertificateEnvironment = 'BadCertificateEnvironment',
badCollapseId = 'BadCollapseId',
badDeviceToken = 'BadDeviceToken',
badExpirationDate = 'BadExpirationDate',
badMessageId = 'BadMessageId',
badPath = 'BadPath',
badPriority = 'BadPriority',
badTopic = 'BadTopic',
deviceTokenNotForTopic = 'DeviceTokenNotForTopic',
duplicateHeaders = 'DuplicateHeaders',
error = 'Error',
expiredProviderToken = 'ExpiredProviderToken',
forbidden = 'Forbidden',
idleTimeout = 'IdleTimeout',
internalServerError = 'InternalServerError',
invalidProviderToken = 'InvalidProviderToken',
invalidPushType = 'InvalidPushType',
invalidSigningKey = 'InvalidSigningKey',
methodNotAllowed = 'MethodNotAllowed',
missingDeviceToken = 'MissingDeviceToken',
missingProviderToken = 'MissingProviderToken',
missingTopic = 'MissingTopic',
payloadEmpty = 'PayloadEmpty',
payloadTooLarge = 'PayloadTooLarge',
serviceUnavailable = 'ServiceUnavailable',
shutdown = 'Shutdown',
tooManyProviderTokenUpdates = 'TooManyProviderTokenUpdates',
tooManyRequests = 'TooManyRequests',
topicDisallowed = 'TopicDisallowed',
unknownError = 'UnknownError',
unregistered = 'Unregistered',
badCertificate = "BadCertificate",
badCertificateEnvironment = "BadCertificateEnvironment",
badCollapseId = "BadCollapseId",
badDeviceToken = "BadDeviceToken",
badExpirationDate = "BadExpirationDate",
badMessageId = "BadMessageId",
badPath = "BadPath",
badPriority = "BadPriority",
badTopic = "BadTopic",
deviceTokenNotForTopic = "DeviceTokenNotForTopic",
duplicateHeaders = "DuplicateHeaders",
error = "Error",
expiredProviderToken = "ExpiredProviderToken",
forbidden = "Forbidden",
idleTimeout = "IdleTimeout",
internalServerError = "InternalServerError",
invalidProviderToken = "InvalidProviderToken",
invalidPushType = "InvalidPushType",
invalidSigningKey = "InvalidSigningKey",
methodNotAllowed = "MethodNotAllowed",
missingDeviceToken = "MissingDeviceToken",
missingProviderToken = "MissingProviderToken",
missingTopic = "MissingTopic",
payloadEmpty = "PayloadEmpty",
payloadTooLarge = "PayloadTooLarge",
serviceUnavailable = "ServiceUnavailable",
shutdown = "Shutdown",
tooManyProviderTokenUpdates = "TooManyProviderTokenUpdates",
tooManyRequests = "TooManyRequests",
topicDisallowed = "TopicDisallowed",
unknownError = "UnknownError",
unregistered = "Unregistered",
}
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './apns'
export * from './errors'
export * from './notifications/notification'
export * from './notifications/silent-notification'
export * from "./apns"
export * from "./errors"
export * from "./notifications/notification"
export * from "./notifications/silent-notification"
18 changes: 9 additions & 9 deletions src/notifications/constants/push-type.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export enum PushType {
alert = 'alert',
background = 'background',
voip = 'voip',
complication = 'complication',
fileprovider = 'fileprovider',
mdm = 'mdm',
liveactivity = 'liveactivity',
location = 'location',
pushtotalk = 'pushtotalk',
alert = "alert",
background = "background",
voip = "voip",
complication = "complication",
fileprovider = "fileprovider",
mdm = "mdm",
liveactivity = "liveactivity",
location = "location",
pushtotalk = "pushtotalk",
}
22 changes: 11 additions & 11 deletions src/notifications/notification.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Priority } from './constants/priority'
import { PushType } from './constants/push-type'
import { Priority } from "./constants/priority"
import { PushType } from "./constants/push-type"

export { PushType, Priority }

Expand Down Expand Up @@ -59,28 +59,28 @@ export class Notification {
}

// Check for "silent" notification
if (typeof this.options.contentAvailable === 'boolean') {
result.aps['content-available'] = this.options.contentAvailable ? 1 : 0
if (typeof this.options.contentAvailable === "boolean") {
result.aps["content-available"] = this.options.contentAvailable ? 1 : 0
}

// Check for sound
if (typeof this.options.sound === 'string' || typeof this.options.sound === 'object') {
if (typeof this.options.sound === "string" || typeof this.options.sound === "object") {
result.aps.sound = this.options.sound
}

// Check for category
if (typeof this.options.category === 'string') {
if (typeof this.options.category === "string") {
result.aps.category = this.options.category
}

// Check for badge
if (typeof this.options.badge === 'number') {
if (typeof this.options.badge === "number") {
result.aps.badge = this.options.badge
}

// Check for threadId
if (typeof this.options.threadId === 'string') {
result.aps['thread-id'] = this.options.threadId
if (typeof this.options.threadId === "string") {
result.aps["thread-id"] = this.options.threadId
}

// Add optional message data
Expand All @@ -89,8 +89,8 @@ export class Notification {
}

// Check for mutable content
if (typeof this.options.mutableContent === 'boolean') {
result.aps['mutable-content'] = this.options.mutableContent ? 1 : 0
if (typeof this.options.mutableContent === "boolean") {
result.aps["mutable-content"] = this.options.mutableContent ? 1 : 0
}

return result
Expand Down
4 changes: 2 additions & 2 deletions src/notifications/silent-notification.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Notification, type NotificationOptions, Priority, PushType } from './notification'
import { Notification, type NotificationOptions, Priority, PushType } from "./notification"

export class SilentNotification extends Notification {
constructor(
deviceToken: string,
options: Omit<NotificationOptions, 'type' | 'alert' | 'priority' | 'contentAvailable'> = {},
options: Omit<NotificationOptions, "type" | "alert" | "priority" | "contentAvailable"> = {},
) {
super(deviceToken, {
contentAvailable: true,
Expand Down
Loading

0 comments on commit 5018acd

Please sign in to comment.