Skip to content

Commit

Permalink
remove custom API type, you ain't gonna need it
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyruppel committed Dec 1, 2023
1 parent 2bb72d7 commit f0881a0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,30 @@ const { upload } = createFlickr({
> 💡 Use `examples/oauth.mjs` to quickly set up an OAuth flow and obtain a
> set of credentials
## migrating from previous versions

Previous versions of this SDK depended on [superagent][superagent] for http
requests. This version of the SDK uses node's native `fetch` instead, so ou now
only receive the response body back from an API call. This means **the return
value of an API call will only be the response body, not a superagent Request**

Migrating existin code looks like this:

```js
// old
const res = await flickr.test.login()
console.log(res.body)

// new
const body = await flickr('flickr.test.login')
console.log(body)
```

## advanced

#### configuring fetch

```ts
```js
import { createFlickr, FetchTransport } from 'flickr-sdk'

const transport = new FetchTransport({
Expand All @@ -90,7 +109,7 @@ const { flickr } = createFlickr('<your API key>', transport)

#### testing

```ts
```js
import { createFlickr, MockTransport, NullAuth } from 'flickr-sdk'
import * as assert from 'node:assert'

Expand Down
24 changes: 11 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { API, APIShape } from "./services/rest/api"
import type { Auth, Transport } from "./types"
import { APIKeyAuth } from "./auth/api_key"
import { OAuthAuth, OAuthConfig } from "./auth/oauth"
Expand All @@ -8,34 +7,33 @@ import { Upload, UploadService } from "./services/upload"
import { Replace, ReplaceService } from "./services/replace"
import { FetchTransport } from "./transport/fetch"

export interface FlickrServices<T extends APIShape> {
flickr: Flickr<T>
export interface FlickrServices {
flickr: Flickr
upload: Upload
replace: Replace
}

export interface FlickrServicesWithOAuth<T extends APIShape>
extends FlickrServices<T> {
export interface FlickrServicesWithOAuth extends FlickrServices {
// oauth is only defined if the auth method is oauth
oauth: OAuthService
}

export function createFlickr<T extends APIShape = API>(
export function createFlickr(
apiKey: string,
transport?: Transport,
): FlickrServices<T>
): FlickrServices

export function createFlickr<T extends APIShape = API>(
export function createFlickr(
oauthConfig: OAuthConfig,
transport?: Transport,
): FlickrServicesWithOAuth<T>
): FlickrServicesWithOAuth

export function createFlickr<T extends APIShape = API, A extends Auth = Auth>(
export function createFlickr<A extends Auth = Auth>(
auth: A,
transport?: Transport,
): A extends OAuthAuth ? FlickrServicesWithOAuth<T> : FlickrServices<T>
): A extends OAuthAuth ? FlickrServicesWithOAuth : FlickrServices

export function createFlickr<T extends APIShape = API>(
export function createFlickr(
auth: string | OAuthConfig | Auth,
transport: Transport = new FetchTransport(),
) {
Expand All @@ -62,7 +60,7 @@ export function createFlickr<T extends APIShape = API>(
}

// REST API
const flickr: Flickr<T> = async (method, params) => {
const flickr: Flickr = async (method, params) => {
const service = new FlickrService(transport, auth)
return service.call(method as string, params as Record<string, string>)
}
Expand Down
6 changes: 1 addition & 5 deletions src/services/rest/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,7 @@ import type { FlickrUrlsLookupGroupResponse } from "./flickr.urls.lookupGroup.re
import type { FlickrUrlsLookupUserParams } from "./flickr.urls.lookupUser"
import type { FlickrUrlsLookupUserResponse } from "./flickr.urls.lookupUser.response"

export type APIShape = {
[key: string]: [params: any, response: any]
}

export type API = APIShape & {
export type API = {
"flickr.activity.userComments": [
FlickrActivityUserCommentsParams,
FlickrActivityUserCommentsResponse,
Expand Down
6 changes: 3 additions & 3 deletions src/services/rest/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Auth, Transport } from "../../types"
import { POST_REGEXP, API, APIShape } from "./api"
import { POST_REGEXP, API } from "./api"
import { GET, POST } from "../../params"
import { JSONParser } from "../../parser/json"

export interface Flickr<A extends APIShape = API> {
<T extends keyof A>(method: T, params: A[T][0]): Promise<A[T][1]>
export interface Flickr {
<T extends keyof API>(method: T, params: API[T][0]): Promise<API[T][1]>
}

export class FlickrService {
Expand Down

0 comments on commit f0881a0

Please sign in to comment.