-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from commercelayer/more-examples
Add more examples
- Loading branch information
Showing
22 changed files
with
1,342 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { core } from '@commercelayer/js-auth' | ||
|
||
const auth = await core.authentication('client_credentials', { | ||
clientId: 'BISG8bb3GWpC8_D7Nt1SuWWdieS5bJq831A50LgB_Ig', | ||
slug: 'demo-store', | ||
scope: 'market:11279' | ||
}) | ||
|
||
console.log(auth) |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"private": true, | ||
"name": "bun", | ||
"type": "module", | ||
"main": "index.ts", | ||
"scripts": { | ||
"start": "bun run index.ts" | ||
}, | ||
"dependencies": { | ||
"@commercelayer/js-auth": "^5.2.0" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# "Bun" example | ||
|
||
## Get started | ||
|
||
First of all you need to install [Bun](https://bun.sh/) on your machine. | ||
|
||
```sh | ||
bun run index.ts | ||
|
||
# or | ||
|
||
bun start | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# "CommonJS" example | ||
|
||
## Get started | ||
|
||
First of all you need to install [Node.js](https://nodejs.org/en) on your machine. | ||
|
||
```sh | ||
node index.js | ||
|
||
# or | ||
|
||
npm start | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# http://editorconfig.org | ||
root = true | ||
|
||
[*] | ||
indent_style = tab | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.yml] | ||
indent_style = space |
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 |
---|---|---|
@@ -0,0 +1,172 @@ | ||
# Logs | ||
|
||
logs | ||
_.log | ||
npm-debug.log_ | ||
yarn-debug.log* | ||
yarn-error.log* | ||
lerna-debug.log* | ||
.pnpm-debug.log* | ||
|
||
# Diagnostic reports (https://nodejs.org/api/report.html) | ||
|
||
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json | ||
|
||
# Runtime data | ||
|
||
pids | ||
_.pid | ||
_.seed | ||
\*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
|
||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
|
||
coverage | ||
\*.lcov | ||
|
||
# nyc test coverage | ||
|
||
.nyc_output | ||
|
||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) | ||
|
||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
|
||
bower_components | ||
|
||
# node-waf configuration | ||
|
||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
|
||
build/Release | ||
|
||
# Dependency directories | ||
|
||
node_modules/ | ||
jspm_packages/ | ||
|
||
# Snowpack dependency directory (https://snowpack.dev/) | ||
|
||
web_modules/ | ||
|
||
# TypeScript cache | ||
|
||
\*.tsbuildinfo | ||
|
||
# Optional npm cache directory | ||
|
||
.npm | ||
|
||
# Optional eslint cache | ||
|
||
.eslintcache | ||
|
||
# Optional stylelint cache | ||
|
||
.stylelintcache | ||
|
||
# Microbundle cache | ||
|
||
.rpt2_cache/ | ||
.rts2_cache_cjs/ | ||
.rts2_cache_es/ | ||
.rts2_cache_umd/ | ||
|
||
# Optional REPL history | ||
|
||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
|
||
\*.tgz | ||
|
||
# Yarn Integrity file | ||
|
||
.yarn-integrity | ||
|
||
# dotenv environment variable files | ||
|
||
.env | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
.env.local | ||
|
||
# parcel-bundler cache (https://parceljs.org/) | ||
|
||
.cache | ||
.parcel-cache | ||
|
||
# Next.js build output | ||
|
||
.next | ||
out | ||
|
||
# Nuxt.js build / generate output | ||
|
||
.nuxt | ||
dist | ||
|
||
# Gatsby files | ||
|
||
.cache/ | ||
|
||
# Comment in the public line in if your project uses Gatsby and not Next.js | ||
|
||
# https://nextjs.org/blog/next-9-1#public-directory-support | ||
|
||
# public | ||
|
||
# vuepress build output | ||
|
||
.vuepress/dist | ||
|
||
# vuepress v2.x temp and cache directory | ||
|
||
.temp | ||
.cache | ||
|
||
# Docusaurus cache and generated files | ||
|
||
.docusaurus | ||
|
||
# Serverless directories | ||
|
||
.serverless/ | ||
|
||
# FuseBox cache | ||
|
||
.fusebox/ | ||
|
||
# DynamoDB Local files | ||
|
||
.dynamodb/ | ||
|
||
# TernJS port file | ||
|
||
.tern-port | ||
|
||
# Stores VSCode versions used for testing VSCode extensions | ||
|
||
.vscode-test | ||
|
||
# yarn v2 | ||
|
||
.yarn/cache | ||
.yarn/unplugged | ||
.yarn/build-state.yml | ||
.yarn/install-state.gz | ||
.pnp.\* | ||
|
||
# wrangler project | ||
|
||
.dev.vars | ||
.wrangler/ |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"printWidth": 140, | ||
"singleQuote": true, | ||
"semi": true, | ||
"useTabs": true | ||
} |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "cloudflare-workers", | ||
"private": true, | ||
"scripts": { | ||
"deploy": "wrangler deploy", | ||
"dev": "wrangler dev", | ||
"start": "wrangler dev", | ||
"test": "vitest" | ||
}, | ||
"dependencies": { | ||
"@commercelayer/js-auth": "workspace:^5.0.0" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/vitest-pool-workers": "^0.1.0", | ||
"@cloudflare/workers-types": "^4.20240314.0", | ||
"typescript": "^5.0.4", | ||
"vitest": "1.3.0", | ||
"wrangler": "^3.0.0" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# CloudFlare Workers | ||
|
||
## Example from scratch | ||
|
||
To create this example from scratch you can just follow the official documentation at [developers.cloudflare.com](https://developers.cloudflare.com/workers/get-started/guide/). | ||
|
||
```sh | ||
pnpm create cloudflare@latest | ||
|
||
# using create-cloudflare version 2.14.2 | ||
# | ||
# ╭ Create an application with Cloudflare | ||
# │ | ||
# ├ In which directory do you want to create your application? | ||
# │ dir ./commercelayer-js-auth | ||
# │ | ||
# ├ What type of application do you want to create? | ||
# │ type "Hello World" Worker | ||
# │ | ||
# ├ Do you want to use TypeScript? | ||
# │ yes typescript | ||
# │ | ||
# │ ... | ||
# │ | ||
# ╰ Application created | ||
# | ||
# ╭ Deploy with Cloudflare | ||
# │ | ||
# ├ Do you want to deploy your application? | ||
# │ yes deploy via `pnpm run deploy` | ||
# │ | ||
# │ ... | ||
# │ | ||
# ├ SUCCESS - View your deployed application at https://commercelayer-js-auth.marcomontalbano.workers.dev | ||
# │ | ||
# │ Navigate to the new directory `cd commercelayer-js-auth` | ||
# │ Run the development server `pnpm run start` | ||
# │ Deploy your application `pnpm run deploy` | ||
# │ Read the documentation https://developers.cloudflare.com/workers | ||
# │ Stuck? Join us at https://discord.gg/cloudflaredev | ||
# │ | ||
# ╰ See you again soon! | ||
``` | ||
|
||
```sh | ||
pnpm start | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Welcome to Cloudflare Workers! This is your first worker. | ||
* | ||
* - Run `npm run dev` in your terminal to start a development server | ||
* - Open a browser tab at http://localhost:8787/ to see your worker in action | ||
* - Run `npm run deploy` to publish your worker | ||
* | ||
* Learn more at https://developers.cloudflare.com/workers/ | ||
*/ | ||
|
||
import { core } from '@commercelayer/js-auth' | ||
|
||
export interface Env { | ||
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/ | ||
// MY_KV_NAMESPACE: KVNamespace; | ||
// | ||
// Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/ | ||
// MY_DURABLE_OBJECT: DurableObjectNamespace; | ||
// | ||
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/ | ||
// MY_BUCKET: R2Bucket; | ||
// | ||
// Example binding to a Service. Learn more at https://developers.cloudflare.com/workers/runtime-apis/service-bindings/ | ||
// MY_SERVICE: Fetcher; | ||
// | ||
// Example binding to a Queue. Learn more at https://developers.cloudflare.com/queues/javascript-apis/ | ||
// MY_QUEUE: Queue; | ||
} | ||
|
||
export default { | ||
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> { | ||
const auth = await core.authentication('client_credentials', { | ||
clientId: 'BISG8bb3GWpC8_D7Nt1SuWWdieS5bJq831A50LgB_Ig', | ||
slug: 'demo-store', | ||
scope: 'market:11279' | ||
}) | ||
|
||
return new Response(`Hello World!\n\nThis is your token: ${auth.accessToken}`); | ||
}, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// test/index.spec.ts | ||
import { env, createExecutionContext, waitOnExecutionContext, SELF } from "cloudflare:test"; | ||
import { describe, it, expect } from "vitest"; | ||
import worker from "../src/index"; | ||
|
||
// For now, you'll need to do something like this to get a correctly-typed | ||
// `Request` to pass to `worker.fetch()`. | ||
const IncomingRequest = Request<unknown, IncomingRequestCfProperties>; | ||
|
||
describe("Hello World worker", () => { | ||
it("responds with Hello World! (unit style)", async () => { | ||
const request = new IncomingRequest("http://example.com"); | ||
// Create an empty context to pass to `worker.fetch()`. | ||
const ctx = createExecutionContext(); | ||
const response = await worker.fetch(request, env, ctx); | ||
// Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions | ||
await waitOnExecutionContext(ctx); | ||
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`); | ||
}); | ||
|
||
it("responds with Hello World! (integration style)", async () => { | ||
const response = await SELF.fetch("https://example.com"); | ||
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"types": [ | ||
"@cloudflare/workers-types/experimental", | ||
"@cloudflare/vitest-pool-workers" | ||
] | ||
}, | ||
"include": ["./**/*.ts", "../src/env.d.ts"], | ||
"exclude": [] | ||
} |
Oops, something went wrong.