This repository was archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 386
[WIP][Admin API Client] Add Admin API client package #1022
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
026ef49
Add Admin API client package
scottdixon 6e9e21a
Update README, remove browser bundle
scottdixon 0a3bd24
Remove SDK headers
scottdixon 682659a
use graphql-client utils
scottdixon e2490ef
Clean up
scottdixon 9aaa4d2
Add userAgentPrefix param
scottdixon 243a0f1
API -> Api
scottdixon 562dd09
Empty changeset
scottdixon bc897b1
Logger, retries, updated graphql-client: new types and validations
scottdixon ae13245
Remove UMD build
scottdixon 54e9faf
v0.0.1
scottdixon 5d8e3ce
Bring back global eslint config :face-palm:
scottdixon bbcbf5d
Remove TS ignore
scottdixon a4f4982
README update
scottdixon 3c4ca58
Logger and retry tests
scottdixon 45e2162
UMD/CJS clean up
scottdixon bb00656
customHeaders -> headers
scottdixon 3e97fa2
README tweak
scottdixon 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
--- |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
node_modules/ | ||
dist/ | ||
|
||
package-lock.json | ||
.vscode/ | ||
.DS_Store | ||
.rollup.cache/ | ||
|
||
# ignore any locally packed packages | ||
*.tgz | ||
!*.d.ts |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"presets": [ | ||
["@babel/preset-env"], | ||
["@shopify/babel-preset", {"typescript": true}] | ||
], | ||
"plugins": [ | ||
["@babel/plugin-transform-runtime"], | ||
["@babel/plugin-transform-async-to-generator"] | ||
] | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
{ | ||
"name": "@shopify/admin-api-client", | ||
"version": "0.0.1", | ||
"description": "Shopify Admin API Client - A lightweight JS client to interact with Shopify's Admin API", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Shopify/shopify-api-js.git" | ||
}, | ||
"author": "Shopify", | ||
"license": "MIT", | ||
"main": "./dist/admin-api-client.min.js", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/admin-api-client.d.ts", | ||
"exports": { | ||
".": { | ||
"module": { | ||
"types": "./dist/ts/index.d.ts", | ||
"default": "./dist/index.mjs" | ||
}, | ||
"import": { | ||
"types": "./dist/ts/index.d.ts", | ||
"default": "./dist/index.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/ts/index.d.ts", | ||
"default": "./dist/index.js" | ||
}, | ||
"default": "./dist/index.mjs" | ||
} | ||
}, | ||
"scripts": { | ||
"lint": "eslint . --ext .js,.ts", | ||
"build": "yarn run rollup", | ||
"test": "jest", | ||
"test:ci": "yarn test", | ||
"rollup": "rollup -c --bundleConfigAsCjs", | ||
"clean": "rimraf dist/*", | ||
"changeset": "changeset", | ||
"version": "changeset version", | ||
"release": "yarn build && changeset publish" | ||
}, | ||
"jest": { | ||
"setupFilesAfterEnv": [ | ||
"./src/tests/setupTests.ts" | ||
], | ||
"transform": { | ||
".*": "babel-jest" | ||
} | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"keywords": [ | ||
"shopify", | ||
"node", | ||
"graphql", | ||
"admin API" | ||
], | ||
"files": [ | ||
"dist/**/*.*" | ||
], | ||
"dependencies": { | ||
"@shopify/graphql-client": "^0.7.0" | ||
}, | ||
"devDependencies": { | ||
scottdixon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"@babel/core": "^7.21.3", | ||
"@babel/plugin-transform-async-to-generator": "^7.20.7", | ||
"@babel/plugin-transform-runtime": "^7.21.0", | ||
"@babel/preset-env": "^7.20.2", | ||
"@babel/preset-typescript": "^7.21.0", | ||
"@changesets/changelog-github": "^0.4.8", | ||
"@changesets/cli": "^2.26.1", | ||
"@rollup/plugin-babel": "^6.0.3", | ||
"@rollup/plugin-commonjs": "^24.0.1", | ||
"@rollup/plugin-eslint": "^9.0.3", | ||
"@rollup/plugin-node-resolve": "^15.0.1", | ||
"@rollup/plugin-replace": "^5.0.2", | ||
"@rollup/plugin-terser": "^0.4.0", | ||
"@rollup/plugin-typescript": "^11.0.0", | ||
"@shopify/babel-preset": "^25.0.0", | ||
"@shopify/eslint-plugin": "^42.0.3", | ||
"@shopify/prettier-config": "^1.1.2", | ||
"@shopify/typescript-configs": "^5.1.0", | ||
"@types/jest": "^29.5.0", | ||
"@types/regenerator-runtime": "^0.13.1", | ||
"@typescript-eslint/parser": "^6.7.5", | ||
"babel-jest": "^29.5.0", | ||
"eslint": "^8.51.0", | ||
"jest": "^29.7.0", | ||
"jest-environment-jsdom": "^29.5.0", | ||
"jest-fetch-mock": "^3.0.3", | ||
"prettier": "^2.5.1", | ||
"regenerator-runtime": "^0.13.11", | ||
"rollup": "^3.19.1", | ||
"rollup-plugin-dts": "^5.2.0", | ||
"tslib": "^2.5.0", | ||
"typescript": "^5.2.0" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/Shopify/shopify-api-js/issues" | ||
}, | ||
"homepage": "https://github.com/Shopify/shopify-api-js/packages/admin-api-client#readme" | ||
} |
scottdixon marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import dts from "rollup-plugin-dts"; | ||
import typescript from "@rollup/plugin-typescript"; | ||
import resolve from "@rollup/plugin-node-resolve"; | ||
import commonjs from "@rollup/plugin-commonjs"; | ||
import terser from "@rollup/plugin-terser"; | ||
import replace from "@rollup/plugin-replace"; | ||
|
||
import * as pkg from "./package.json"; | ||
|
||
export const mainSrcInput = "src/index.ts"; | ||
|
||
export function getPlugins({ tsconfig, minify } = {}) { | ||
return [ | ||
replace({ | ||
preventAssignment: true, | ||
ROLLUP_REPLACE_CLIENT_VERSION: pkg.version, | ||
}), | ||
resolve(), | ||
commonjs(), | ||
typescript({ | ||
tsconfig: tsconfig ? tsconfig : "./tsconfig.build.json", | ||
outDir: "./dist/ts", | ||
}), | ||
...(minify === true ? [terser({ keep_fnames: new RegExp("fetch") })] : []), | ||
]; | ||
} | ||
|
||
const config = [ | ||
{ | ||
input: mainSrcInput, | ||
plugins: getPlugins(), | ||
output: [ | ||
{ | ||
dir: "./dist", | ||
format: "es", | ||
sourcemap: true, | ||
preserveModules: true, | ||
preserveModulesRoot: "src", | ||
entryFileNames: "[name].mjs", | ||
}, | ||
], | ||
}, | ||
{ | ||
input: mainSrcInput, | ||
plugins: getPlugins(), | ||
output: [ | ||
{ | ||
dir: "./dist", | ||
format: "cjs", | ||
sourcemap: true, | ||
exports: "named", | ||
preserveModules: true, | ||
preserveModulesRoot: "src", | ||
}, | ||
], | ||
}, | ||
{ | ||
input: "./dist/ts/index.d.ts", | ||
output: [{ file: "dist/admin-api-client.d.ts", format: "es" }], | ||
plugins: [dts.default()], | ||
}, | ||
]; | ||
|
||
export default config; |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { | ||
createGraphQLClient, | ||
getCurrentSupportedApiVersions, | ||
validateApiVersion, | ||
validateDomainAndGetStoreUrl, | ||
generateGetGQLClientParams, | ||
generateGetHeaders, | ||
ApiClientRequestParams, | ||
} from "@shopify/graphql-client"; | ||
|
||
import { | ||
AdminApiClientOptions, | ||
AdminApiClient, | ||
AdminApiClientConfig, | ||
} from "./types"; | ||
import { | ||
DEFAULT_CONTENT_TYPE, | ||
ACCESS_TOKEN_HEADER, | ||
CLIENT, | ||
DEFAULT_CLIENT_VERSION, | ||
} from "./constants"; | ||
import { | ||
validateRequiredAccessToken, | ||
validateServerSideUsage, | ||
} from "./validations"; | ||
|
||
export function createAdminApiClient({ | ||
storeDomain, | ||
apiVersion, | ||
accessToken, | ||
userAgentPrefix, | ||
retries = 0, | ||
customFetchApi: clientFetchApi, | ||
scottdixon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logger, | ||
}: AdminApiClientOptions): AdminApiClient { | ||
const currentSupportedApiVersions = getCurrentSupportedApiVersions(); | ||
|
||
const storeUrl = validateDomainAndGetStoreUrl({ | ||
client: CLIENT, | ||
storeDomain, | ||
}); | ||
|
||
const baseApiVersionValidationParams = { | ||
client: CLIENT, | ||
currentSupportedApiVersions, | ||
logger, | ||
}; | ||
|
||
validateServerSideUsage(); | ||
validateApiVersion({ | ||
client: CLIENT, | ||
currentSupportedApiVersions, | ||
apiVersion, | ||
logger, | ||
}); | ||
validateRequiredAccessToken(accessToken); | ||
|
||
const apiUrlFormatter = generateApiUrlFormatter( | ||
storeUrl, | ||
apiVersion, | ||
baseApiVersionValidationParams | ||
); | ||
|
||
const config: AdminApiClientConfig = { | ||
storeDomain: storeUrl, | ||
apiVersion, | ||
accessToken, | ||
headers: { | ||
"Content-Type": DEFAULT_CONTENT_TYPE, | ||
Accept: DEFAULT_CONTENT_TYPE, | ||
[ACCESS_TOKEN_HEADER]: accessToken, | ||
"User-Agent": `${ | ||
userAgentPrefix ? `${userAgentPrefix} | ` : "" | ||
}${CLIENT} v${DEFAULT_CLIENT_VERSION}`, | ||
Comment on lines
+72
to
+74
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. This is a mouthful 😅 is there a cleaner approach? 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. Can't think of anything other than pulling the prefix part into its own variable, but that doesn't really change anything. Fine as is IMO. |
||
}, | ||
apiUrl: apiUrlFormatter(), | ||
userAgentPrefix, | ||
}; | ||
|
||
const graphqlClient = createGraphQLClient({ | ||
headers: config.headers, | ||
url: config.apiUrl, | ||
retries, | ||
fetchApi: clientFetchApi, | ||
logger, | ||
}); | ||
|
||
const getHeaders = generateGetHeaders(config); | ||
const getApiUrl = generateGetApiUrl(config, apiUrlFormatter); | ||
|
||
const getGQLClientParams = generateGetGQLClientParams({ | ||
getHeaders, | ||
getApiUrl, | ||
}); | ||
|
||
const fetch = (...props: ApiClientRequestParams) => { | ||
return graphqlClient.fetch(...getGQLClientParams(...props)); | ||
}; | ||
|
||
const request = <TData>(...props: ApiClientRequestParams) => { | ||
return graphqlClient.request<TData>(...getGQLClientParams(...props)); | ||
}; | ||
|
||
const client: AdminApiClient = { | ||
config, | ||
getHeaders, | ||
getApiUrl, | ||
fetch, | ||
request, | ||
}; | ||
|
||
return Object.freeze(client); | ||
} | ||
|
||
function generateApiUrlFormatter( | ||
storeUrl: string, | ||
defaultApiVersion: string, | ||
baseApiVersionValidationParams: Omit< | ||
Parameters<typeof validateApiVersion>[0], | ||
"apiVersion" | ||
> | ||
) { | ||
return (apiVersion?: string) => { | ||
if (apiVersion) { | ||
validateApiVersion({ | ||
...baseApiVersionValidationParams, | ||
apiVersion, | ||
}); | ||
} | ||
|
||
const urlApiVersion = (apiVersion ?? defaultApiVersion).trim(); | ||
|
||
return `${storeUrl}/admin/api/${urlApiVersion}/graphql.json`; | ||
}; | ||
} | ||
|
||
function generateGetApiUrl( | ||
config: AdminApiClientConfig, | ||
apiUrlFormatter: (version?: string) => string | ||
): AdminApiClient["getApiUrl"] { | ||
return (propApiVersion?: string) => { | ||
return propApiVersion ? apiUrlFormatter(propApiVersion) : config.apiUrl; | ||
}; | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const DEFAULT_CONTENT_TYPE = "application/json"; | ||
// This is value is replaced with package.json version during rollup build process | ||
export const DEFAULT_CLIENT_VERSION = "ROLLUP_REPLACE_CLIENT_VERSION"; | ||
export const ACCESS_TOKEN_HEADER = "X-Shopify-Access-Token"; | ||
export const CLIENT = "Admin API Client"; |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { createAdminApiClient } from "./admin-api-client"; |
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.
Do we want to have
require
entries for the CJS build? I think that would be the right setup here?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.
I think that's happening a few lines down - let me know if I'm missing something!