-
Notifications
You must be signed in to change notification settings - Fork 255
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
switch to generated type declarations #2327
Open
djskinner
wants to merge
9
commits into
integration/typescript
Choose a base branch
from
plat-12947-8
base: integration/typescript
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
49d3129
switch to generated type declarations
1653316
Merge branch 'plat-12947-7' into plat-12947-8
834e673
skip constructor when adding client methods
b32a393
Merge branch 'integration/typescript' into plat-12947-8
fc89d05
reinstate map
43724bd
prefer ts-expect-error
cdbf54e
Merge branch 'integration/typescript' into plat-12947-8
d7014b9
prefer ts-expect-error
2d00ced
remove unused ts directives
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,66 +2,22 @@ import configSchema from './config' | |
import Event from './event' | ||
import Breadcrumb from './breadcrumb' | ||
import Session from './session' | ||
import map from './lib/es-utils/map' | ||
import includes from './lib/es-utils/includes' | ||
import filter from './lib/es-utils/filter' | ||
import reduce from './lib/es-utils/reduce' | ||
import keys from './lib/es-utils/keys' | ||
import assign from './lib/es-utils/assign' | ||
import runCallbacks from './lib/callback-runner' | ||
import metadataDelegate from './lib/metadata-delegate' | ||
import runSyncCallbacks from './lib/sync-callback-runner' | ||
import BREADCRUMB_TYPES from './lib/breadcrumb-types' | ||
import { add, clear, merge } from './lib/feature-flag-delegate' | ||
import { App, BreadcrumbType, Config, Device, FeatureFlag, OnBreadcrumbCallback, OnErrorCallback, OnSessionCallback, Plugin, User } from './common' | ||
import { BreadcrumbType, Config, Delivery, FeatureFlag, LoggerConfig, Notifier, OnBreadcrumbCallback, OnErrorCallback, OnSessionCallback, Plugin, SessionDelegate, User } from './common' | ||
|
||
const noop = () => {} | ||
|
||
interface LoggerConfig { | ||
debug: (msg: any) => void | ||
info: (msg: any) => void | ||
warn: (msg: any) => void | ||
error: (msg: any, err?: unknown) => void | ||
} | ||
|
||
interface Notifier { | ||
name: string | ||
version: string | ||
url: string | ||
} | ||
|
||
interface EventDeliveryPayload { | ||
apiKey: string | ||
notifier: Notifier | ||
events: Event[] | ||
} | ||
|
||
interface SessionDeliveryPayload { | ||
notifier?: Notifier | ||
device?: Device | ||
app?: App | ||
sessions?: Array<{ | ||
id: string | ||
startedAt: Date | ||
user?: User | ||
}> | ||
} | ||
|
||
interface Delivery { | ||
sendEvent(payload: EventDeliveryPayload, cb: (err?: Error | null) => void): void | ||
sendSession(session: SessionDeliveryPayload, cb: (err?: Error | null) => void): void | ||
} | ||
|
||
export interface SessionDelegate<T extends Config = Config> { | ||
startSession: (client: Client<T>, session: Session) => Client | ||
pauseSession: (client: Client<T>) => void | ||
resumeSession: (client: Client<T>) => Client | ||
} | ||
|
||
export default class Client<T extends Config = Config> { | ||
private readonly _notifier?: Notifier | ||
public readonly _notifier?: Notifier | ||
// This ought to be Required<T> but the current version of TypeScript doesn't seem to like it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment can go now |
||
public readonly _config: Required<Config> | ||
public readonly _config: T & Required<Config> | ||
private readonly _schema: any | ||
|
||
public _delivery: Delivery | ||
|
@@ -87,13 +43,14 @@ export default class Client<T extends Config = Config> { | |
b: OnBreadcrumbCallback[] | ||
} | ||
|
||
private readonly Client: typeof Client | ||
private readonly Event: typeof Event | ||
private readonly Breadcrumb: typeof Breadcrumb | ||
private readonly Session: typeof Session | ||
public readonly Client: typeof Client | ||
public readonly Event: typeof Event | ||
public readonly Breadcrumb: typeof Breadcrumb | ||
public readonly Session: typeof Session | ||
|
||
private readonly _depth: number | ||
public _depth: number | ||
|
||
public _pausedSession?: Session | null | ||
public _sessionDelegate?: SessionDelegate | ||
|
||
constructor (configuration: T, schema = configSchema, internalPlugins: Plugin<T>[] = [], notifier?: Notifier) { | ||
|
@@ -139,7 +96,7 @@ export default class Client<T extends Config = Config> { | |
this.Session = Session | ||
|
||
this._config = this._configure(configuration, internalPlugins) | ||
map(internalPlugins.concat(this._config.plugins), pl => { | ||
internalPlugins.concat(this._config.plugins).map(pl => { | ||
if (pl) this._loadPlugin(pl) | ||
}) | ||
|
||
|
@@ -195,7 +152,7 @@ export default class Client<T extends Config = Config> { | |
this._context = c | ||
} | ||
|
||
_configure (opts: T, internalPlugins: Plugin[]) { | ||
_configure (opts: T, internalPlugins: Plugin<T>[]) { | ||
const schema = reduce(internalPlugins, (schema, plugin) => { | ||
if (plugin && plugin.configSchema) return assign({}, schema, plugin.configSchema) | ||
return schema | ||
|
@@ -207,7 +164,7 @@ export default class Client<T extends Config = Config> { | |
} | ||
|
||
// accumulate configuration and error messages | ||
const { errors, config } = reduce(keys(schema), (accum, key: keyof typeof opts) => { | ||
const { errors, config } = reduce(Object.keys(schema) as unknown as (keyof T)[], (accum, key: keyof typeof opts) => { | ||
const defaultValue = schema[key].defaultValue(opts[key]) | ||
|
||
if (opts[key] !== undefined) { | ||
|
@@ -249,7 +206,7 @@ export default class Client<T extends Config = Config> { | |
if (config.onSession) this._cbs.s = this._cbs.s.concat(config.onSession) | ||
|
||
// finally warn about any invalid config where we fell back to the default | ||
if (keys(errors).length) { | ||
if (Object.keys(errors).length) { | ||
this._logger.warn(generateConfigErrorMessage(errors, opts)) | ||
} | ||
|
||
|
@@ -369,7 +326,7 @@ export default class Client<T extends Config = Config> { | |
return types === null || includes(types, type) | ||
} | ||
|
||
notify (maybeError: Error, onError?: OnErrorCallback, postReportCallback: (err: Error | null | undefined, event: Event) => void = noop) { | ||
notify (maybeError: Error | string, onError?: OnErrorCallback, postReportCallback: (err: Error | null | undefined, event: Event) => void = noop) { | ||
const event = Event.create(maybeError, true, undefined, 'notify()', this._depth + 1, this._logger) | ||
this._notify(event, onError, postReportCallback) | ||
} | ||
|
@@ -411,11 +368,8 @@ export default class Client<T extends Config = Config> { | |
|
||
if (this._isBreadcrumbTypeEnabled('error')) { | ||
// only leave a crumb for the error if actually got sent | ||
// @ts-ignore | ||
Client.prototype.leaveBreadcrumb.call(this, event.errors[0].errorClass, { | ||
// @ts-ignore | ||
errorClass: event.errors[0].errorClass, | ||
// @ts-ignore | ||
errorMessage: event.errors[0].errorMessage, | ||
severity: event.severity | ||
}, 'error') | ||
|
@@ -447,7 +401,7 @@ export default class Client<T extends Config = Config> { | |
|
||
const generateConfigErrorMessage = (errors: Record<string, Error>, rawInput: Config) => { | ||
const er = new Error( | ||
`Invalid configuration\n${map(keys(errors), key => ` - ${key} ${errors[key]}, got ${stringify(rawInput[key])}`).join('\n\n')}`) | ||
`Invalid configuration\n${(Object.keys(errors) as unknown as (keyof Config)[]).map(key => ` - ${key} ${errors[key]}, got ${stringify(rawInput[key])}`).join('\n\n')}`) | ||
return er | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,10 +60,6 @@ export interface Logger { | |
error: (...args: any[]) => void | ||
} | ||
|
||
export interface SessionDelegate { | ||
startSession: (client: Client) => Client | ||
} | ||
|
||
export interface EventPayload { | ||
apiKey: string | ||
notifier: { | ||
|
@@ -130,9 +126,9 @@ export interface Request { | |
} | ||
|
||
export interface User { | ||
id?: string | null | ||
email?: string | null | ||
name?: string | null | ||
id?: string | null | undefined | ||
email?: string | null | undefined | ||
name?: string | null | undefined | ||
Comment on lines
+129
to
+131
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't the |
||
} | ||
|
||
type ThreadType = 'cocoa' | 'android' | 'browserJs' | ||
|
@@ -157,4 +153,50 @@ export interface Stackframe { | |
export interface FeatureFlag { | ||
name: string | ||
variant?: string | null | ||
} | ||
|
||
export interface LoggerConfig { | ||
debug: (msg: any) => void | ||
info: (msg: any) => void | ||
warn: (msg: any) => void | ||
error: (msg: any, err?: unknown) => void | ||
} | ||
|
||
export interface Notifier { | ||
name: string | ||
version: string | ||
url: string | ||
} | ||
|
||
export interface EventDeliveryPayload { | ||
apiKey: string | ||
notifier: Notifier | ||
events: Event[] | ||
} | ||
|
||
export interface SessionDeliveryPayload { | ||
notifier?: Notifier | ||
device?: Device | ||
app?: App | ||
sessions?: Array<{ | ||
id: string | ||
startedAt: Date | ||
user?: User | ||
}> | ||
} | ||
export interface Delivery { | ||
sendEvent(payload: EventDeliveryPayload, cb: (err?: Error | null) => void): void | ||
sendSession(session: SessionDeliveryPayload, cb: (err?: Error | null) => void): void | ||
} | ||
|
||
export interface SessionDelegate<T extends Config = Config> { | ||
startSession: (client: Client<T>, session: Session) => Client | ||
pauseSession: (client: Client<T>) => void | ||
resumeSession: (client: Client<T>) => Client | ||
} | ||
|
||
export interface BugsnagStatic extends Client { | ||
start(apiKeyOrOpts: string | Config): Client | ||
createClient(apiKeyOrOpts: string | Config): Client | ||
isStarted(): boolean | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One downside to having babel transpile from TypeScript is that it's not type aware. So if you do:
It fails because
LoggerConfig
is only a type.Due to our requirement to support lower TS versions we couldn't do
export { type LoggerConfig } from './common'
which does fix the issue.So I moved all the types to
common.js
which are exported like `export * from './common'`` and there are no complaints.However, the generated types that TyeScript is now producing still do include the
type
modifier:And this can't be solved by us internally not using the
type
modifier.To get around this we'll have to use https://www.npmjs.com/package/downlevel-dts as a post-processing step on the types.
An added benefit of this is that internally we'll be able to use
type
modifier and whatever else we like that is supported by our more recent version of TypeScriptThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fwiw, we can use the
type
modifier, but not inline.export type { LoggerConfig } from './common'
works with TS 3.8, but notexport { type LoggerConfig } from './common'