Skip to content

Commit

Permalink
christmas patches
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodes committed Dec 24, 2024
1 parent e7ab850 commit 21c618f
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 32 deletions.
54 changes: 40 additions & 14 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
// rollup.config.js
import { terser } from 'rollup-plugin-terser';
// import { eslint } from 'rollup-plugin-eslint';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import babel from '@rollup/plugin-babel';
import pkg from './package.json';

const input = ['src/storefront.js'];
const inputFiles = ['src/storefront.js', 'src/resolver.js'];

export default [
// Base UMD config for storefront.js
{
// umd
input,
input: 'src/storefront.js',
plugins: [
nodeResolve({
browser: true,
modulesOnly: true,
}),
babel({
babelHelpers: 'bundled',
}),
babelHelpers: 'bundled',
}),
terser(),
],
output: [
Expand All @@ -36,18 +34,46 @@ export default [
include: ['lib/**'],
},
},
// Additional UMD config for resolver.js
{
// esm and cjs
input,
input: 'src/resolver.js',
plugins: [
nodeResolve({
browser: true,
modulesOnly: true,
}),
babel({
babelHelpers: 'bundled',
}),
terser()
babel({
babelHelpers: 'bundled',
}),
terser(),
],
output: [
{
file: `dist/${pkg.name}/resolver.min.js`,
format: 'umd',
name: '@fleetbase/storefront/resolver',
esModule: false,
exports: 'named',
sourcemap: true,
},
],
watch: {
exclude: ['node_modules/**'],
include: ['lib/**'],
},
},
// Config for ESM and CJS
{
input: inputFiles,
plugins: [
nodeResolve({
browser: true,
modulesOnly: true,
}),
babel({
babelHelpers: 'bundled',
}),
terser(),
],
output: [
{
Expand All @@ -63,6 +89,6 @@ export default [
sourcemap: true,
},
],
external: ['axios']
external: ['axios'],
},
];
43 changes: 30 additions & 13 deletions src/resolver.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
import { Product, Category, Customer, Cart, DeliveryServiceQuote, Store, StoreLocation, StoreHour, Checkout, Review } from './resources';
import { BrowserAdapter, NodeAdapter, pluralize, singularize } from '@fleetbase/sdk';

const resources = {
Product, Category, Customer, Cart, DeliveryServiceQuote, Store, StoreLocation, StoreHour, Checkout, Review
export const resources = {
Product,
Category,
Customer,
Cart,
DeliveryServiceQuote,
Store,
StoreLocation,
StoreHour,
Checkout,
Review,
};

const adapters = {
BrowserAdapter,
NodeAdapter
NodeAdapter,
};

function toStudlyCase(str) {
return str
.split(/[^a-zA-Z0-9]/) // Split by non-alphanumeric characters
.filter(Boolean) // Remove empty elements
.map((word) => word.charAt(0).toUpperCase() + word.slice(1)) // Capitalize the first letter of each word
.join(''); // Join the words together without spaces
}

class Resolver {
constructor () {
constructor() {
this.resources = resources;
this.adapters = adapters;

Expand All @@ -20,25 +37,25 @@ class Resolver {

lookup(type, className) {
const key = pluralize(type);
const params = [ ...arguments ].slice(2);
const classKey = toStudlyCase(className);
const params = [...arguments].slice(2);

if (!this[key]) {
throw new Error('Attempted to resolve invalid type');
}

if (!this[key][className]) {
throw new Error(`No ${singularize(type)} named ${className} to resolve`);
if (!this[key][classKey]) {
throw new Error(`No ${singularize(type)} named ${className} (${classKey}) to resolve`);
}

return new this[key][className](...params);
return new this[key][classKey](...params);
}
}

const lookup = function() {
const lookup = function () {
return new Resolver(...arguments);
};

export {
Resolver,
lookup
};
export { Resolver, lookup };

export default Resolver;
57 changes: 53 additions & 4 deletions src/resources/checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@ import Resource from '../resource';
import Product from './product';
import { StoreActions, isResource, Order } from '@fleetbase/sdk';

const objectToParams = (obj = {}, prefix = null) => {
if (obj === null || obj === undefined) {
return {};
}

const parsed = {};
const keys = Object.keys(obj);

for (let index = 0; index < keys.length; index++) {
const key = keys[index];
const value = obj[key];

if (key && value) {
if (prefix && typeof prefix === 'string') {
parsed[`${prefix}[${key}]`] = value;
continue;
}

parsed[key] = value;
}
}

return parsed;
};

const checkoutActions = new StoreActions({
create: undefined,
findAll: undefined,
Expand All @@ -24,13 +49,37 @@ const checkoutActions = new StoreActions({
if (isResource(gateway)) {
gateway = gateway.getAttribute('code');
}
return this.adapter.get(`${this.namespace}/before`, { ...orderOptions, customer, cart, serviceQuote, gateway}, options);

return this.adapter.get(`${this.namespace}/before`, { ...orderOptions, customer, cart, serviceQuote, gateway }, options);
},

captureOrder: function (token, transactionDetails = {}, options = {}) {
return this.adapter.get(`${this.namespace}/capture`, { token, transactionDetails }, options).then(orderJson => new Order(orderJson));
}
return this.adapter.post(`${this.namespace}/capture`, { token, transactionDetails }, options).then((orderJson) => new Order(orderJson));
},

createStripeSetupIntent: function (customer, options = {}) {
if (isResource(customer)) {
customer = customer.id;
}

return this.adapter.post(`${this.namespace}/stripe-setup-intent`, { customer }, options);
},

updateStripePaymentIntent: function (paymentIntent, customer, cart, serviceQuote, orderOptions = {}, options = {}) {
if (isResource(customer)) {
customer = customer.id;
}

if (isResource(cart)) {
cart = cart.id;
}

if (isResource(serviceQuote)) {
serviceQuote = serviceQuote.id;
}

return this.adapter.put(`${this.namespace}/stripe-update-payment-intent`, { paymentIntent, customer, cart, serviceQuote, ...orderOptions }, options);
},
});

export default class Checkout extends Resource {
Expand Down
12 changes: 12 additions & 0 deletions src/resources/customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ export default class Customer extends Resource {
return new Collection(orders.map((attributes) => new Order(attributes, this.adapter)));
});
}

getStripeEphemeralKey(params = {}) {
return this.adapter
.setHeaders({ 'Customer-Token': this.token })
.post('customers/stripe-ephemeral-key', params);
}

getStripeSetupIntent(params = {}) {
return this.adapter
.setHeaders({ 'Customer-Token': this.token })
.post('customers/stripe-setup-intent', params);
}
}

export { customerActions };
8 changes: 7 additions & 1 deletion src/storefront.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import StorefrontStore from './store';
import { detectAdapter, isKeyValid } from './utils';
import { lookup } from './resolver';
import { Product, Category, Customer, Cart, Store, StoreLocation, StoreHour, DeliveryServiceQuote, Checkout, PaymentGateway, Review, Network } from './resources';
import { cartActions } from './resources/cart';
import { customerActions } from './resources/customer';
Expand Down Expand Up @@ -51,6 +52,11 @@ export default class Storefront {
return this.adapter.get('about');
}

/** lookup a specific store or network provided the ID */
lookup(id) {
return this.adapter.get(`lookup/${id}`);
}

/** search products in store or network */
search(query, options = {}) {
return this.adapter.get('search', { query, ...options }).then(products => {
Expand All @@ -71,4 +77,4 @@ export default class Storefront {
}
}

export { Product, Category, Customer, Cart, Store, StoreLocation, StoreHour, DeliveryServiceQuote, Checkout, PaymentGateway, Review, Network };
export { Product, Category, Customer, Cart, Store, StoreLocation, StoreHour, DeliveryServiceQuote, Checkout, PaymentGateway, Review, Network, lookup };

0 comments on commit 21c618f

Please sign in to comment.