Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/real-rabbits-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@audius/sdk': major
---

Create SDK without services by default in all cases

Updates the `sdk()` constructor to create the SDK without the old services and API structure regardless of config. This will align all configurations to have the same API surface that matches existing docs and examples.

To maintain compatibility, older legacy apps can use `createSdkWithServices()` instead of `sdk()` when initting the SDK. This is not advised for third-party apps.
4 changes: 2 additions & 2 deletions packages/commands/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
sdk,
createSdkWithServices,
SolanaRelay,
Configuration,
createHedgehogWalletClient,
Expand Down Expand Up @@ -163,7 +163,7 @@ export const initializeAudiusSdk = async ({

const audiusWalletClient = createHedgehogWalletClient(getHedgehog())

audiusSdk = sdk({
audiusSdk = createSdkWithServices({
appName: 'audius-cmd',
environment: 'development',
services: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { logger } from './logger'
import { initializeDiscoveryDb } from '@pedalboard/basekit'
import { connectWeb3 } from './web3'
import { app } from './server'
import { AudiusSdk, sdk } from '@audius/sdk'
import { AudiusSdk, createSdkWithServices } from '@audius/sdk'

export type SharedData = {
config: Config
Expand All @@ -30,12 +30,9 @@ export let wallets: WalletManager
export let audiusSdk: AudiusSdk

const main = async () => {
audiusSdk = sdk({
audiusSdk = createSdkWithServices({
appName: 'relay',
environment:
config.environment === 'dev'
? 'development'
: 'production'
environment: config.environment === 'dev' ? 'development' : 'production'
})
try {
// async config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
SolanaRelayWalletAdapter,
SolanaClient,
SolanaRelay,
sdk
createSdkWithServices
} from '@audius/sdk'

const makeAAOSelector = () =>
Expand Down Expand Up @@ -62,7 +62,7 @@ export const audiusSdk = ({
const solanaClient = solanaRpcEndpoint
? makeSolanaClient(solanaRelay, solanaRpcEndpoint)
: undefined
return sdk({
return createSdkWithServices({
appName: 'trending-challenge-rewards',
apiKey,
apiSecret,
Expand Down
9 changes: 7 additions & 2 deletions packages/mobile/src/services/sdk/audius-sdk.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { EventEmitter } from 'events'

import type { AudiusSdk } from '@audius/sdk'
import { Configuration, SolanaRelay, sdk, ArchiverService } from '@audius/sdk'
import {
Configuration,
SolanaRelay,
createSdkWithServices,
ArchiverService
} from '@audius/sdk'

import { env } from 'app/services/env'

Expand Down Expand Up @@ -53,7 +58,7 @@ const initSdk = async () => {
// Overrides some DN configuration from optimizely
const audiusWalletClient = await getAudiusWalletClient()

const audiusSdk = sdk({
const audiusSdk = createSdkWithServices({
appName: env.APP_NAME,
apiKey: env.API_KEY,
environment: env.ENVIRONMENT,
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/sdk/api/uploads/UploadsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class UploadsApi {
private readonly storage

constructor(services: UploadsApiServicesConfig) {
this.storage = services.storageService
this.storage = services.storage
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/sdk/api/uploads/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { StorageService } from '../../services'

export type UploadsApiServicesConfig = {
storageService: StorageService
storage: StorageService
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fetch from 'cross-fetch'

import { ResolveApi } from './api/ResolveApi'
import {
ChallengesApi,
CoinsApi,
Expand All @@ -12,7 +13,6 @@ import {
NotificationsApi,
PlaylistsApi,
PrizesApi,
ResolveApi,
RewardsApi,
SearchApi,
TipsApi,
Expand All @@ -32,9 +32,10 @@ import {
import { OAuth } from './oauth'
import { OAuthTokenStore } from './oauth/tokenStore'
import { Logger, Storage, StorageNodeSelector } from './services'
import { type SdkConfig } from './types'
import { SdkConfigSchema, type SdkConfig } from './types'

export const createSdkWithoutServices = (config: SdkConfig) => {
export const createSdk = (config: SdkConfig) => {
SdkConfigSchema.parse(config)
const { services, environment } = config

const appName = 'appName' in config ? config.appName : undefined
Expand Down Expand Up @@ -65,7 +66,8 @@ export const createSdkWithoutServices = (config: SdkConfig) => {
? new OAuth({
apiKey,
tokenStore,
basePath
basePath,
logger
})
: undefined

Expand Down Expand Up @@ -116,6 +118,7 @@ export const createSdkWithoutServices = (config: SdkConfig) => {

// Initialize API clients
const usersApi = new UsersApi(apiConfig)
const resolveApi = new ResolveApi(apiConfig)

return {
oauth,
Expand All @@ -125,7 +128,7 @@ export const createSdkWithoutServices = (config: SdkConfig) => {
// albums
playlists: new PlaylistsApi(apiConfig),
tips: new TipsApi(apiConfig),
resolve: new ResolveApi(apiConfig),
resolve: resolveApi.resolve.bind(resolveApi),
// chats
// grants
developerApps: new DeveloperAppsApi(apiConfig),
Expand All @@ -142,7 +145,7 @@ export const createSdkWithoutServices = (config: SdkConfig) => {
challenges: new ChallengesApi(apiConfig),
prizes: new PrizesApi(apiConfig),
uploads: new UploadsApi({
storageService:
storage:
services?.storage ??
new Storage({
storageNodeSelector:
Expand Down
20 changes: 17 additions & 3 deletions packages/sdk/src/sdk/createSdkWithServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { GrantsApi } from './api/grants/GrantsApi'
import { NotificationsApi } from './api/notifications/NotificationsApi'
import { PlaylistsApi } from './api/playlists/PlaylistsApi'
import { TracksApi } from './api/tracks/TracksApi'
import { UploadsApi } from './api/uploads/UploadsApi'
import { UsersApi } from './api/users/UsersApi'
import { developmentConfig } from './config/development'
import { productionConfig } from './config/production'
Expand Down Expand Up @@ -89,13 +90,23 @@ import {
StorageNodeSelector,
getDefaultStorageNodeSelectorConfig
} from './services/StorageNodeSelector'
import { SdkConfig, ServicesContainer } from './types'
import { SdkConfig, SdkConfigSchema, ServicesContainer } from './types'
import fetch from './utils/fetch'

const ensureHex = (str: string): Hex =>
str.startsWith('0x') ? (str as Hex) : `0x${str}`

/**
* Creates an instance of the SDK that predates the delegated writes support of
* the API server. Handles all the writes locally and relays them. Supports
* wallet actions like purchases, tipping, and DMs/chats but requires passing
* in an AudiusWalletClient.
*
* Not recommended for third party use.
*/
export const createSdkWithServices = (config: SdkConfig) => {
SdkConfigSchema.parse(config)

Comment thread
rickyrombo marked this conversation as resolved.
const isBrowser: boolean =
typeof window !== 'undefined' && typeof window.document !== 'undefined'

Expand Down Expand Up @@ -460,7 +471,8 @@ const initializeApis = ({
? new OAuth({
apiKey,
tokenStore,
basePath
basePath,
logger: services.logger
})
: undefined

Expand Down Expand Up @@ -521,6 +533,7 @@ const initializeApis = ({
const events = new EventsApi(apiClientConfig, services)
const explore = new ExploreApi(apiClientConfig)
const search = new SearchApi(apiClientConfig)
const uploads = new UploadsApi(services)

return {
oauth,
Expand All @@ -545,6 +558,7 @@ const initializeApis = ({
coins,
wallets,
challenges,
prizes
prizes,
uploads
}
}
2 changes: 2 additions & 0 deletions packages/sdk/src/sdk/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable import/export */
export { sdk } from './sdk'
export { createSdk } from './createSdk'
export { createSdkWithServices } from './createSdkWithServices'
export type { AudiusSdk } from './sdk'
export * from './api/generated/default'
export { TracksApi } from './api/tracks/TracksApi'
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/sdk/scripts/manageRewardsLookupTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import untildify from 'untildify'
import { developmentConfig } from '../config/development'
import { productionConfig } from '../config/production'
import type { SdkServicesConfig } from '../config/types'
import { sdk as audiusSdk } from '../sdk'
import { Logger } from '../services'
import type { SdkConfig } from '../types'
import { createSdkWithServices } from '../createSdkWithServices'

/**
* Derives the sender addresses for Validators and Anti Abuse Oracles
Expand Down Expand Up @@ -158,7 +158,7 @@ const createLookupTable = async ({
)
)
)
const sdk = audiusSdk({
const sdk = createSdkWithServices({
appName: 'generate-rewards-lookup-table',
environment,
services: {
Expand Down Expand Up @@ -276,7 +276,7 @@ const updateLookupTable = async ({
)
)
)
const sdk = audiusSdk({
const sdk = createSdkWithServices({
appName: 'generate-rewards-lookup-table',
environment,
services: {
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/sdk/scripts/verifyUser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { program } from 'commander'

import { sdk as audiusSdk } from '../sdk'
import { createSdkWithServices } from '../createSdkWithServices'
import { EntityManagerAction, EntityType } from '../services'
import { HashId } from '../types/HashId'

Expand Down Expand Up @@ -37,7 +37,7 @@ program
process.exit(1)
}

const sdk = audiusSdk({
const sdk = createSdkWithServices({
appName: 'verify-user',
apiSecret: args.privateKey,
environment: args.environment
Expand Down
63 changes: 2 additions & 61 deletions packages/sdk/src/sdk/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,8 @@
import { createSdkWithServices } from './createSdkWithServices'
import { createSdkWithoutServices } from './createSdkWithoutServices'
import {
SdkConfig,
SdkConfigSchema,
type SdkWithApiKeyOnlyConfig,
type SdkWithApiSecretConfig,
type SdkWithBearerTokenConfig,
type SdkWithAppNameOnlyConfig,
DevAppSchemaWithApiSecret,
DevAppSchemaWithApiKeyOnly,
DevAppSchemaWithAppNameOnly,
DevAppSchemaWithBearerToken
} from './types'

const createSdkWithBearerToken = (config: SdkWithBearerTokenConfig) => {
DevAppSchemaWithBearerToken.parse(config)
return createSdkWithoutServices(config)
}

const createSdkWithApiName = (config: SdkWithAppNameOnlyConfig) => {
DevAppSchemaWithAppNameOnly.parse(config)
return createSdkWithServices(config)
}

const createSdkWithApiKey = (config: SdkWithApiKeyOnlyConfig) => {
DevAppSchemaWithApiKeyOnly.parse(config)
return createSdkWithServices(config)
}

const createSdkWithApiSecret = (config: SdkWithApiSecretConfig) => {
DevAppSchemaWithApiSecret.parse(config)
return createSdkWithServices(config)
}
import { createSdk } from './createSdk'

/**
* The Audius SDK
*/
export function sdk(
config: SdkWithBearerTokenConfig
): ReturnType<typeof createSdkWithBearerToken>
export function sdk(
config: SdkWithApiSecretConfig
): ReturnType<typeof createSdkWithApiSecret>
export function sdk(
config: SdkWithApiKeyOnlyConfig
): ReturnType<typeof createSdkWithApiKey>
export function sdk(
config: SdkWithAppNameOnlyConfig
): ReturnType<typeof createSdkWithApiName>
export function sdk(config: SdkConfig) {
SdkConfigSchema.parse(config)

if ('bearerToken' in config) {
return createSdkWithBearerToken(config)
}
if ('apiSecret' in config) {
return createSdkWithApiSecret(config)
}
if ('apiKey' in config) {
return createSdkWithApiKey(config)
}

return createSdkWithApiName(config)
}
export const sdk = createSdk

export type AudiusSdk = ReturnType<typeof sdk>
Loading
Loading